示例#1
0
 public function updateRuleMultiProductData(Mage_CatalogRule_Model_Rule $rule, $pIds)
 {
     $ruleId = $rule->getId();
     $write = $this->_getWriteAdapter();
     $write->beginTransaction();
     $write->delete($this->getTable('catalogrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId));
     if (!$rule->getIsActive()) {
         $write->commit();
         return $this;
     }
     $websiteIds = $rule->getWebsiteIds();
     if (empty($websiteIds)) {
         $write->commit();
         return $this;
     }
     if (!is_array($websiteIds)) {
         $websiteIds = explode(',', $websiteIds);
     }
     Varien_Profiler::start('__MATCH_PRODUCTS__');
     $productIds = $rule->getMatchingMultiProductIds($pIds);
     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();
     $queryStart = 'INSERT INTO ' . $this->getTable('catalogrule/rule_product') . ' (
             rule_id, from_time, to_time, website_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 ($customerGroupIds as $customerGroupId) {
                     $rows[] = "('" . implode("','", array($ruleId, $fromTime, $toTime, $websiteId, $customerGroupId, $productId, $actionOperator, $actionAmount, $actionStop, $sortOrder)) . "')";
                     /**
                      * Array with 1000 rows contain about 2M data
                      */
                     if (sizeof($rows) == 1000) {
                         $sql = $queryStart . join(',', $rows) . $queryEnd;
                         $write->query($sql);
                         $rows = array();
                     }
                 }
             }
         }
         if (!empty($rows)) {
             $sql = $queryStart . join(',', $rows) . $queryEnd;
             $write->query($sql);
         }
         $write->commit();
     } catch (Exception $e) {
         $write->rollback();
         throw $e;
     }
     return $this;
 }