/** * @param Rule $rule * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function updateRuleProductData(Rule $rule) { $ruleId = $rule->getId(); if ($rule->getProductsFilter()) { $this->connection->delete($this->getTable('catalogrule_product'), ['rule_id=?' => $ruleId, 'product_id IN (?)' => $rule->getProductsFilter()]); } else { $this->connection->delete($this->getTable('catalogrule_product'), $this->connection->quoteInto('rule_id=?', $ruleId)); } if (!$rule->getIsActive()) { return $this; } $websiteIds = $rule->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = explode(',', $websiteIds); } if (empty($websiteIds)) { return $this; } \Magento\Framework\Profiler::start('__MATCH_PRODUCTS__'); $productIds = $rule->getMatchingProductIds(); \Magento\Framework\Profiler::stop('__MATCH_PRODUCTS__'); $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(); $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : ''; $subActionAmount = $rule->getSubDiscountAmount(); $actionStop = $rule->getStopRulesProcessing(); $rows = []; foreach ($productIds as $productId => $validationByWebsite) { foreach ($websiteIds as $websiteId) { if (empty($validationByWebsite[$websiteId])) { continue; } 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) { $this->connection->insertMultiple($this->getTable('catalogrule_product'), $rows); $rows = []; } } } } if (!empty($rows)) { $this->connection->insertMultiple($this->getTable('catalogrule_product'), $rows); } return $this; }
/** * 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; }