public function testAddSalesRuleNameToOrder()
 {
     $observer = $this->getMock('Magento\\Framework\\Event\\Observer', ['getOrder'], [], '', false);
     $rule = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['load', 'getName', '__wakeup'], [], '', false);
     $order = $this->getMock('Magento\\Sales\\Model\\Order', ['setCouponRuleName', 'getCouponCode', '__wakeup'], [], '', false);
     $couponCode = 'coupon code';
     $ruleId = 1;
     $observer->expects($this->any())->method('getOrder')->will($this->returnValue($order));
     $order->expects($this->once())->method('getCouponCode')->will($this->returnValue($couponCode));
     $this->couponMock->expects($this->once())->method('getRuleId')->will($this->returnValue($ruleId));
     $this->ruleFactory->expects($this->once())->method('create')->will($this->returnValue($rule));
     $rule->expects($this->once())->method('load')->with($ruleId)->will($this->returnSelf());
     $order->expects($this->once())->method('setCouponRuleName');
     $this->assertEquals($this->model, $this->model->execute($observer));
 }
예제 #2
0
 /**
  * @param $mailType
  * @param $mail
  * @param $name
  * @param $couponCode
  * @param $storeId
  */
 public function saveMail($mailType, $mail, $name, $couponCode, $storeId)
 {
     if ($couponCode != '') {
         $coupon = $this->_coupon->loadByCode($couponCode);
         $rule = $this->_rule->load($coupon->getRuleId());
         $couponAmount = $rule->getDiscountAmount();
         switch ($rule->getSimpleAction()) {
             case 'cart_fixed':
                 $couponType = 1;
                 break;
             case 'by_percent':
                 $couponType = 2;
                 break;
             default:
                 $couponType = 0;
                 break;
         }
     } else {
         $couponType = 0;
         $couponAmount = 0;
     }
     $sent = $this->_mailsent;
     $date = $this->_dateTime;
     $sent->setMailType($mailType)->setStoreId($storeId)->setCustomerEmail($mail)->setCustomerName($name)->setCouponNumber($couponCode)->setCouponType($couponType)->setCouponAmount($couponAmount)->setSentAt($date->gmtDate())->save();
 }
 /**
  * Add coupon's rule name to order data
  *
  * @param EventObserver $observer
  * @return $this
  */
 public function execute(EventObserver $observer)
 {
     $order = $observer->getOrder();
     $couponCode = $order->getCouponCode();
     if (empty($couponCode)) {
         return $this;
     }
     $this->_coupon->loadByCode($couponCode);
     $ruleId = $this->_coupon->getRuleId();
     if (empty($ruleId)) {
         return $this;
     }
     /** @var \Magento\SalesRule\Model\Rule $rule */
     $rule = $this->_ruleFactory->create()->load($ruleId);
     $order->setCouponRuleName($rule->getName());
     return $this;
 }
예제 #4
0
 /**
  * Load primary coupon (is_primary = 1) for specified rule
  *
  *
  * @param \Magento\SalesRule\Model\Coupon $object
  * @param \Magento\SalesRule\Model\Rule|int $rule
  * @return bool
  */
 public function loadPrimaryByRule(\Magento\SalesRule\Model\Coupon $object, $rule)
 {
     $connection = $this->getConnection();
     if ($rule instanceof \Magento\SalesRule\Model\Rule) {
         $ruleId = $rule->getId();
     } else {
         $ruleId = (int) $rule;
     }
     $select = $connection->select()->from($this->getMainTable())->where('rule_id = :rule_id')->where('is_primary = :is_primary');
     $data = $connection->fetchRow($select, [':rule_id' => $ruleId, ':is_primary' => 1]);
     if (!$data) {
         return false;
     }
     $object->setData($data);
     $this->_afterLoad($object);
     return true;
 }
 /**
  * @param EventObserver $observer
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute(EventObserver $observer)
 {
     $order = $observer->getEvent()->getOrder();
     if (!$order || $order->getDiscountAmount() == 0) {
         return $this;
     }
     // lookup rule ids
     $ruleIds = explode(',', $order->getAppliedRuleIds());
     $ruleIds = array_unique($ruleIds);
     $ruleCustomer = null;
     $customerId = $order->getCustomerId();
     // use each rule (and apply to customer, if applicable)
     foreach ($ruleIds as $ruleId) {
         if (!$ruleId) {
             continue;
         }
         /** @var \Magento\SalesRule\Model\Rule $rule */
         $rule = $this->_ruleFactory->create();
         $rule->load($ruleId);
         if ($rule->getId()) {
             $rule->loadCouponCode();
             $rule->setTimesUsed($rule->getTimesUsed() + 1);
             $rule->save();
             if ($customerId) {
                 /** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
                 $ruleCustomer = $this->_ruleCustomerFactory->create();
                 $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
                 if ($ruleCustomer->getId()) {
                     $ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed() + 1);
                 } else {
                     $ruleCustomer->setCustomerId($customerId)->setRuleId($ruleId)->setTimesUsed(1);
                 }
                 $ruleCustomer->save();
             }
         }
     }
     $this->_coupon->load($order->getCouponCode(), 'code');
     if ($this->_coupon->getId()) {
         $this->_coupon->setTimesUsed($this->_coupon->getTimesUsed() + 1);
         $this->_coupon->save();
         if ($customerId) {
             $this->_couponUsage->updateCustomerCouponTimesUsed($customerId, $this->_coupon->getId());
         }
     }
     return $this;
 }
 /**
  * @param int|bool $ruleCustomerId
  * @dataProvider salesOrderAfterPlaceDataProvider
  */
 public function testSalesOrderAfterPlace($ruleCustomerId)
 {
     $observer = $this->getMock('Magento\\Framework\\Event\\Observer', [], [], '', false);
     $rule = $this->getMock('Magento\\SalesRule\\Model\\Rule', [], [], '', false);
     $ruleCustomer = $this->getMock('Magento\\SalesRule\\Model\\Rule\\Customer', ['setCustomerId', 'loadByCustomerRule', 'getId', 'setTimesUsed', 'setRuleId', 'save', '__wakeup'], [], '', false);
     $order = $this->initOrderFromEvent($observer);
     $ruleId = 1;
     $couponId = 1;
     $customerId = 1;
     $discountAmount = 10;
     $order->expects($this->once())->method('getAppliedRuleIds')->will($this->returnValue($ruleId));
     $order->expects($this->once())->method('getDiscountAmount')->will($this->returnValue($discountAmount));
     $order->expects($this->once())->method('getCustomerId')->will($this->returnValue($customerId));
     $this->ruleFactory->expects($this->once())->method('create')->will($this->returnValue($rule));
     $rule->expects($this->once())->method('getId')->will($this->returnValue($ruleId));
     $this->ruleCustomerFactory->expects($this->once())->method('create')->will($this->returnValue($ruleCustomer));
     $ruleCustomer->expects($this->once())->method('getId')->will($this->returnValue($ruleCustomerId));
     $ruleCustomer->expects($this->any())->method('setCustomerId')->will($this->returnSelf());
     $ruleCustomer->expects($this->any())->method('setRuleId')->will($this->returnSelf());
     $this->couponMock->expects($this->any())->method('getId')->will($this->returnValue($couponId));
     $this->couponUsage->expects($this->once())->method('updateCustomerCouponTimesUsed')->with($customerId, $couponId);
     $this->assertEquals($this->model, $this->model->execute($observer));
 }
예제 #7
0
 /**
  * Retrieve rule's primary coupon
  *
  * @return \Magento\SalesRule\Model\Coupon
  */
 public function getPrimaryCoupon()
 {
     if ($this->_primaryCoupon === null) {
         $this->_primaryCoupon = $this->_couponFactory->create();
         $this->_primaryCoupon->loadPrimaryByRule($this->getId());
         $this->_primaryCoupon->setRule($this)->setIsPrimary(true);
     }
     return $this->_primaryCoupon;
 }
예제 #8
0
 /**
  * Run test loadByCode method
  */
 public function testLoadByCode()
 {
     $this->eventManager->expects($this->any())->method('dispatch');
     $this->resourceMock->expects($this->once())->method('load');
     $this->assertEquals($this->couponModel, $this->couponModel->loadByCode('code-value'));
 }