/**
  * @param \Magento\CatalogRule\Model\Rule $rule
  * @param \Closure $proceed
  * @param \Magento\Framework\DataObject|\Magento\Catalog\Model\Product $product
  * @return bool
  */
 public function aroundValidate(\Magento\CatalogRule\Model\Rule $rule, \Closure $proceed, \Magento\Framework\DataObject $product)
 {
     $validateResult = $proceed($product);
     if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) {
         foreach ($configurableProducts as $configurableProductId) {
             $validateResult = $rule->getConditions()->validateByEntityId($configurableProductId);
             // If any of configurable product is valid for current rule, then their sub-product must be valid too
             if ($validateResult) {
                 break;
             }
         }
     }
     return $validateResult;
 }
Exemple #2
0
 /**
  * Apply catalog rule to product
  *
  * @param ModelRule $rule
  * @param Product $product
  * @param array $websiteIds
  * @return $this
  * @throws \Exception
  */
 public function applyToProduct($rule, $product, $websiteIds)
 {
     if (!$rule->getIsActive()) {
         return $this;
     }
     $ruleId = $rule->getId();
     $productId = $product->getId();
     $write = $this->_getWriteAdapter();
     $write->beginTransaction();
     $write->delete($this->getTable('catalogrule_product'), array($write->quoteInto('rule_id=?', $ruleId), $write->quoteInto('product_id=?', $productId)));
     if (!$rule->getConditions()->validate($product)) {
         $write->delete($this->getTable('catalogrule_product_price'), array($write->quoteInto('product_id=?', $productId)));
         $write->commit();
         return $this;
     }
     $customerGroupIds = $rule->getCustomerGroupIds();
     $fromTime = strtotime($rule->getFromDate());
     $toTime = strtotime($rule->getToDate());
     $toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
     $sortOrder = (int) $rule->getSortOrder();
     $actionOperator = $rule->getSimpleAction();
     $actionAmount = $rule->getDiscountAmount();
     $actionStop = $rule->getStopRulesProcessing();
     $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     $subActionAmount = $rule->getSubDiscountAmount();
     $rows = array();
     try {
         foreach ($websiteIds as $websiteId) {
             foreach ($customerGroupIds as $customerGroupId) {
                 $rows[] = array('rule_id' => $ruleId, 'from_time' => $fromTime, 'to_time' => $toTime, 'website_id' => $websiteId, 'customer_group_id' => $customerGroupId, 'product_id' => $productId, 'action_operator' => $actionOperator, 'action_amount' => $actionAmount, 'action_stop' => $actionStop, 'sort_order' => $sortOrder, 'sub_simple_action' => $subActionOperator, 'sub_discount_amount' => $subActionAmount);
                 if (count($rows) == 1000) {
                     $write->insertMultiple($this->getTable('catalogrule_product'), $rows);
                     $rows = array();
                 }
             }
         }
         if (!empty($rows)) {
             $write->insertMultiple($this->getTable('catalogrule_product'), $rows);
         }
     } catch (\Exception $e) {
         $write->rollback();
         throw $e;
     }
     $this->applyAllRulesForDateRange(null, null, $product);
     $write->commit();
     return $this;
 }
Exemple #3
0
 /**
  * @param Rule $rule
  * @param Product $product
  * @return $this
  * @throws \Exception
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function applyRule(Rule $rule, $product)
 {
     $ruleId = $rule->getId();
     $productId = $product->getId();
     $websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());
     $write = $this->getWriteAdapter();
     $write->delete($this->resource->getTableName('catalogrule_product'), [$write->quoteInto('rule_id = ?', $ruleId), $write->quoteInto('product_id = ?', $productId)]);
     if (!$rule->getConditions()->validate($product)) {
         $write->delete($this->resource->getTableName('catalogrule_product_price'), [$write->quoteInto('product_id = ?', $productId)]);
         return $this;
     }
     $customerGroupIds = $rule->getCustomerGroupIds();
     $fromTime = strtotime($rule->getFromDate());
     $toTime = strtotime($rule->getToDate());
     $toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
     $sortOrder = (int) $rule->getSortOrder();
     $actionOperator = $rule->getSimpleAction();
     $actionAmount = $rule->getDiscountAmount();
     $actionStop = $rule->getStopRulesProcessing();
     $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     $subActionAmount = $rule->getSubDiscountAmount();
     $rows = [];
     try {
         foreach ($websiteIds as $websiteId) {
             foreach ($customerGroupIds as $customerGroupId) {
                 $rows[] = ['rule_id' => $ruleId, 'from_time' => $fromTime, 'to_time' => $toTime, 'website_id' => $websiteId, 'customer_group_id' => $customerGroupId, 'product_id' => $productId, 'action_operator' => $actionOperator, 'action_amount' => $actionAmount, 'action_stop' => $actionStop, 'sort_order' => $sortOrder, 'sub_simple_action' => $subActionOperator, 'sub_discount_amount' => $subActionAmount];
                 if (count($rows) == $this->batchCount) {
                     $write->insertMultiple($this->getTable('catalogrule_product'), $rows);
                     $rows = [];
                 }
             }
         }
         if (!empty($rows)) {
             $write->insertMultiple($this->resource->getTableName('catalogrule_product'), $rows);
         }
     } catch (\Exception $e) {
         throw $e;
     }
     $this->applyAllRules($product);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function getConditions()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getConditions');
     if (!$pluginInfo) {
         return parent::getConditions();
     } else {
         return $this->___callPlugins('getConditions', func_get_args(), $pluginInfo);
     }
 }