Ejemplo n.º 1
0
 /**
  * Bind sales rule to customer group(s) and website(s).
  * Save rule's associated store labels.
  * Save product attributes used in rule.
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(AbstractModel $object)
 {
     if ($object->hasStoreLabels()) {
         $this->saveStoreLabels($object->getId(), $object->getStoreLabels());
     }
     if ($object->hasWebsiteIds()) {
         $websiteIds = $object->getWebsiteIds();
         if (!is_array($websiteIds)) {
             $websiteIds = explode(',', (string) $websiteIds);
         }
         $this->bindRuleToEntity($object->getId(), $websiteIds, 'website');
     }
     if ($object->hasCustomerGroupIds()) {
         $customerGroupIds = $object->getCustomerGroupIds();
         if (!is_array($customerGroupIds)) {
             $customerGroupIds = explode(',', (string) $customerGroupIds);
         }
         $this->bindRuleToEntity($object->getId(), $customerGroupIds, 'customer_group');
     }
     // Save product attributes used in rule
     $ruleProductAttributes = array_merge($this->getProductAttributes(serialize($object->getConditions()->asArray())), $this->getProductAttributes(serialize($object->getActions()->asArray())));
     if (count($ruleProductAttributes)) {
         $this->setActualProductAttributes($object, $ruleProductAttributes);
     }
     // Update auto geterated specific coupons if exists
     if ($object->getUseAutoGeneration() && $object->hasDataChanges()) {
         $this->_resourceCoupon->updateSpecificCoupons($object);
     }
     return parent::_afterSave($object);
 }
Ejemplo n.º 2
0
 /**
  * Bind sales rule to customer group(s) and website(s).
  * Save rule's associated store labels.
  * Save product attributes used in rule.
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(AbstractModel $object)
 {
     if ($object->hasStoreLabels()) {
         $this->saveStoreLabels($object->getId(), $object->getStoreLabels());
     }
     // Save product attributes used in rule
     $ruleProductAttributes = array_merge($this->getProductAttributes(serialize($object->getConditions()->asArray())), $this->getProductAttributes(serialize($object->getActions()->asArray())));
     if (count($ruleProductAttributes)) {
         $this->setActualProductAttributes($object, $ruleProductAttributes);
     }
     // Update auto geterated specific coupons if exists
     if ($object->getUseAutoGeneration() && $object->hasDataChanges()) {
         $this->_resourceCoupon->updateSpecificCoupons($object);
     }
     return parent::_afterSave($object);
 }
 /**
  * Generates the coupon code based on the code id.
  *
  * @return bool
  */
 public function generateCoupon()
 {
     $params = $this->getRequest()->getParams();
     //check for param code and id
     if (!isset($params['id']) || !isset($params['code'])) {
         $this->helper->log('Coupon no id or code is set');
         return false;
     }
     //coupon rule id
     $couponCodeId = $params['id'];
     if ($couponCodeId) {
         $rule = $this->ruleFactory->create()->load($couponCodeId);
         $generator = $this->massGeneratorFactory->create();
         $generator->setFormat(\Magento\SalesRule\Helper\Coupon::COUPON_FORMAT_ALPHANUMERIC);
         $generator->setRuleId($couponCodeId);
         $generator->setUsesPerCoupon(1);
         $generator->setDash(3);
         $generator->setLength(9);
         $generator->setPrefix('DOT-');
         $generator->setSuffix('');
         //set the generation settings
         $rule->setCouponCodeGenerator($generator);
         $rule->setCouponType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO);
         //generate the coupon
         $coupon = $rule->acquireCoupon();
         $couponCode = $coupon->getCode();
         //save the type of coupon
         /** @var \Magento\SalesRule\Model\Coupon $couponModel */
         $couponModel = $this->couponFactory->create()->loadByCode($couponCode);
         $couponModel->setType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON)->setGeneratedByDotmailer(1);
         if (is_numeric($params['expire_days'])) {
             $expireDate = $this->_localeDate->date()->add(new \DateInterval(sprintf('P%sD', $params['expire_days'])));
             $couponModel->setExpirationDate($expireDate);
         } elseif ($rule->getToDate()) {
             $couponModel->setExpirationDate($rule->getToDate());
         }
         $this->coupon->save($couponModel);
         return $couponCode;
     }
     return false;
 }
Ejemplo n.º 4
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'));
 }
 /**
  * Coupon constructor.
  *
  * @param DateTime                                          $dateTime
  * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  */
 public function __construct(DateTime $dateTime, \Magento\Framework\Model\ResourceModel\Db\Context $context)
 {
     $this->dateTime = $dateTime;
     parent::__construct($context);
 }