/** * Update products which are matched for rule * * @param Mage_CatalogRule_Model_Rule $rule * @return Mage_CatalogRule_Model_Resource_Rule */ public function updateRuleProductData(Mage_CatalogRule_Model_Rule $rule) { $ruleId = $rule->getId(); $write = $this->_getWriteAdapter(); $write->beginTransaction(); if ($rule->getProductsFilter()) { $write->delete($this->getTable('catalogrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId) . $write->quoteInto('and product_id in (?)', implode(',', $rule->getProductsFilter()))); } else { $write->delete($this->getTable('catalogrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId)); } if (!$rule->getIsActive()) { $write->commit(); return $this; } $websiteIds = $rule->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = explode(',', $websiteIds); } if (empty($websiteIds)) { return $this; } Varien_Profiler::start('__MATCH_PRODUCTS__'); $productIds = $rule->getMatchingProductIds(); Varien_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(); $actionStop = $rule->getStopRulesProcessing(); $rows = array(); try { foreach ($productIds as $productId) { 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); if (count($rows) == 1000) { $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows); $rows = array(); } } } } if (!empty($rows)) { $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows); } $write->commit(); } catch (Exception $e) { $write->rollback(); throw $e; } return $this; }
/** * Update products which are matched for rule * * @param Mage_CatalogRule_Model_Rule $rule * * @throws Exception * @return Mage_CatalogRule_Model_Resource_Rule */ public function updateRuleProductData(Mage_CatalogRule_Model_Rule $rule) { $ruleId = $rule->getId(); $write = $this->_getWriteAdapter(); $write->beginTransaction(); if ($rule->getProductsFilter()) { $this->cleanProductData($ruleId, $rule->getProductsFilter()); } else { $this->cleanProductData($ruleId); } if (!$rule->getIsActive()) { $write->commit(); return $this; } $websiteIds = $rule->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = explode(',', $websiteIds); } if (empty($websiteIds)) { return $this; } try { $this->insertRuleData($rule, $websiteIds); $write->commit(); } catch (Exception $e) { $write->rollback(); throw $e; } return $this; }
/** * Update products which are matched for rule * * @param Mage_CatalogRule_Model_Rule $rule * * @return Mage_CatalogRule_Model_Resource_Rule */ public function updateRuleProductData(Mage_CatalogRule_Model_Rule $rule) { $helper = $this->getStorePricingHelper(); $ruleId = $rule->getId(); $write = $this->_getWriteAdapter(); $write->beginTransaction(); if ($this->getVersionHelper()->isGe1600() && $rule->getProductsFilter()) { $write->delete($this->getTable('catalogrule/rule_product'), array('rule_id=?' => $ruleId, 'product_id IN (?)' => $rule->getProductsFilter())); } else { $write->delete($this->getTable('catalogrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId)); } if (!$rule->getIsActive()) { $write->commit(); return $this; } $websiteIds = $rule->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = explode(',', $websiteIds); } if (empty($websiteIds)) { return $this; } $storeIds = $rule->getStoreIds(); if (!is_array($storeIds)) { $storeIds = explode(',', $storeIds); } if (empty($storeIds)) { return $this; } $productIds = $rule->getMatchingProductIds(); $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(); if ($this->getVersionHelper()->isGe1700()) { $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : ''; } $actionStop = $rule->getStopRulesProcessing(); $rows = array(); if (!$this->getVersionHelper()->isGe1600()) { $queryStart = 'INSERT INTO ' . $this->getTable('catalogrule/rule_product') . ' ( rule_id, from_time, to_time, website_id, store_id, customer_group_id, product_id, action_operator, action_amount, action_stop, sort_order ) values '; $queryEnd = ' ON DUPLICATE KEY UPDATE action_operator=VALUES(action_operator), action_amount=VALUES(action_amount), action_stop=VALUES(action_stop)'; } try { foreach ($productIds as $productId) { foreach ($websiteIds as $websiteId) { foreach ($helper->getStoreIdsByWebsiteId($websiteId) as $storeId) { if (!in_array($storeId, $storeIds)) { continue; } foreach ($customerGroupIds as $customerGroupId) { $discountAmount = $rule->getDiscountAmount(); if ($this->getVersionHelper()->isGe1600()) { $row = array('rule_id' => $ruleId, 'from_time' => $fromTime, 'to_time' => $toTime, 'website_id' => $websiteId, 'store_id' => $storeId, 'customer_group_id' => $customerGroupId, 'product_id' => $productId, 'action_operator' => $actionOperator, 'action_amount' => $discountAmount, 'action_stop' => $actionStop, 'sort_order' => $sortOrder); if ($this->getVersionHelper()->isGe1700()) { $subDiscountAmount = $rule->getSubDiscountAmount(); $row['sub_simple_action'] = $subActionOperator; $row['sub_discount_amount'] = $subDiscountAmount; } $rows[] = $row; if (count($rows) == 1000) { $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows); $rows = array(); } } else { $rows[] = "('" . implode("','", array($ruleId, $fromTime, $toTime, $websiteId, $storeId, $customerGroupId, $productId, $actionOperator, $discountAmount, $actionStop, $sortOrder)) . "')"; if (sizeof($rows) == 1000) { $sql = $queryStart . join(',', $rows) . $queryEnd; $write->query($sql); $rows = array(); } } } } } } if ($this->getVersionHelper()->isGe1600()) { if (!empty($rows)) { $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows); } } else { if (!empty($rows)) { $sql = $queryStart . join(',', $rows) . $queryEnd; $write->query($sql); } } $write->commit(); } catch (Exception $e) { $write->rollback(); throw $e; } return $this; }
/** * Update products which are matched for rule * * @param Mage_CatalogRule_Model_Rule $rule * @return Mage_CatalogRule_Model_Resource_Rule */ public function updateProlabelsRuleProductData($rule) { $ruleId = $rule->getId(); $write = $this->_getWriteAdapter(); $write->beginTransaction(); if ($rule->getProductsFilter()) { $write->delete($this->getTable('prolabels/index'), $write->quoteInto('rules_id=?', $ruleId) . $write->quoteInto('and product_id in (?)', implode(',', $rule->getProductsFilter()))); } else { $write->delete($this->getTable('prolabels/index'), $write->quoteInto('rules_id=?', $ruleId)); } // Varien_Profiler::start('__MATCH_PRODUCTS__'); $productIds = $rule->getMatchingProductIds(); // Varien_Profiler::stop('__MATCH_PRODUCTS__'); $rows = array(); try { foreach ($productIds as $productId) { $rows[] = array('rules_id' => $ruleId, 'product_id' => $productId); if (count($rows) == 1000) { $write->insertMultiple($this->getTable('prolabels/index'), $rows); $rows = array(); } } if (!empty($rows)) { $write->insertMultiple($this->getTable('prolabels/index'), $rows); } $write->commit(); } catch (Exception $e) { $write->rollback(); throw $e; } return $this; }