示例#1
0
 /**
  * Get rule associated store Ids
  * 
  * @return array
  */
 public function getStoreIds()
 {
     if ($this->getVersionHelper()->isGe1700()) {
         if (!$this->hasStoreIds()) {
             $storeIds = $this->_getResource()->getStoreIds($this->getId());
             $this->setData('store_ids', (array) $storeIds);
         }
         return $this->_getData('store_ids');
     } else {
         return parent::getStoreIds();
     }
 }
示例#2
0
 /**
  * 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;
 }