示例#1
0
 /**
  * Apply catalog rule to product
  *
  * @param Mage_CatalogRule_Model_Rule $rule
  * @param Mage_Catalog_Model_Product $product
  * @param array $websiteIds
  *
  * @return Mage_CatalogRule_Model_Resource_Rule
  */
 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/rule_product'), array($write->quoteInto('rule_id=?', $ruleId), $write->quoteInto('product_id=?', $productId)));
     if (!$rule->getConditions()->validate($product)) {
         $write->delete($this->getTable('catalogrule/rule_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/rule_product'), $rows);
                     $rows = array();
                 }
             }
         }
         if (!empty($rows)) {
             $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows);
         }
     } catch (Exception $e) {
         $write->rollback();
         throw $e;
     }
     $this->applyAllRulesForDateRange(null, null, $product);
     $write->commit();
     return $this;
 }
示例#2
0
 /**
  * Inserts rule data into catalogrule/rule_product table
  *
  * @param Mage_CatalogRule_Model_Rule $rule
  * @param array $websiteIds
  * @param array $productIds
  */
 public function insertRuleData(Mage_CatalogRule_Model_Rule $rule, array $websiteIds, array $productIds = array())
 {
     /** @var $write Varien_Db_Adapter_Interface */
     $write = $this->_getWriteAdapter();
     $customerGroupIds = $rule->getCustomerGroupIds();
     $fromTime = (int) strtotime($rule->getFromDate());
     $toTime = (int) strtotime($rule->getToDate());
     $toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
     /** @var Mage_Core_Model_Date $coreDate */
     $coreDate = $this->_factory->getModel('core/date');
     $timestamp = $coreDate->gmtTimestamp('Today');
     if ($fromTime > $timestamp || $toTime && $toTime < $timestamp) {
         return;
     }
     $sortOrder = (int) $rule->getSortOrder();
     $actionOperator = $rule->getSimpleAction();
     $actionAmount = (double) $rule->getDiscountAmount();
     $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     $subActionAmount = (double) $rule->getSubDiscountAmount();
     $actionStop = (int) $rule->getStopRulesProcessing();
     /** @var $helper Mage_Catalog_Helper_Product_Flat */
     $helper = $this->_factory->getHelper('catalog/product_flat');
     if ($helper->isEnabled() && $helper->isBuiltAllStores()) {
         /** @var $store Mage_Core_Model_Store */
         foreach ($this->_app->getStores(false) as $store) {
             if (in_array($store->getWebsiteId(), $websiteIds)) {
                 /** @var $selectByStore Varien_Db_Select */
                 $selectByStore = $rule->getProductFlatSelect($store->getId())->joinLeft(array('cg' => $this->getTable('customer/customer_group')), $write->quoteInto('cg.customer_group_id IN (?)', $customerGroupIds), array('cg.customer_group_id'))->reset(Varien_Db_Select::COLUMNS)->columns(array(new Zend_Db_Expr($store->getWebsiteId()), 'cg.customer_group_id', 'p.entity_id', new Zend_Db_Expr($rule->getId()), new Zend_Db_Expr($fromTime), new Zend_Db_Expr($toTime), new Zend_Db_Expr("'" . $actionOperator . "'"), new Zend_Db_Expr($actionAmount), new Zend_Db_Expr($actionStop), new Zend_Db_Expr($sortOrder), new Zend_Db_Expr("'" . $subActionOperator . "'"), new Zend_Db_Expr($subActionAmount)));
                 if (count($productIds) > 0) {
                     $selectByStore->where('p.entity_id IN (?)', array_keys($productIds));
                 }
                 $selects = $write->selectsByRange('entity_id', $selectByStore, self::RANGE_PRODUCT_STEP);
                 foreach ($selects as $select) {
                     $write->query($write->insertFromSelect($select, $this->getTable('catalogrule/rule_product'), array('website_id', 'customer_group_id', 'product_id', 'rule_id', 'from_time', 'to_time', 'action_operator', 'action_amount', 'action_stop', 'sort_order', 'sub_simple_action', 'sub_discount_amount'), Varien_Db_Adapter_Interface::INSERT_IGNORE));
                 }
             }
         }
     } else {
         if (count($productIds) == 0) {
             Varien_Profiler::start('__MATCH_PRODUCTS__');
             $productIds = $rule->getMatchingProductIds();
             Varien_Profiler::stop('__MATCH_PRODUCTS__');
         }
         $rows = array();
         foreach ($productIds as $productId => $validationByWebsite) {
             foreach ($websiteIds as $websiteId) {
                 foreach ($customerGroupIds as $customerGroupId) {
                     if (empty($validationByWebsite[$websiteId])) {
                         continue;
                     }
                     $rows[] = array('rule_id' => $rule->getId(), '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/rule_product'), $rows);
                         $rows = array();
                     }
                 }
             }
         }
         if (!empty($rows)) {
             $write->insertMultiple($this->getTable('catalogrule/rule_product'), $rows);
         }
     }
 }
示例#3
0
 /**
  * Apply catalog rule to product
  * 
  * @param Mage_CatalogRule_Model_Rule $rule
  * @param Mage_Catalog_Model_Product $product
  * @param array $websiteIds
  * @param array $storeIds
  * 
  * @return Mage_CatalogRule_Model_Resource_Rule
  */
 public function applyToProduct2($rule, $product, $websiteIds, $storeIds)
 {
     if (!$rule->getIsActive()) {
         return $this;
     }
     $helper = $this->getStorePricingHelper();
     $ruleId = $rule->getId();
     $productId = $product->getId();
     $write = $this->_getWriteAdapter();
     $write->beginTransaction();
     $write->delete($this->getTable('catalogrule/rule_product'), array($write->quoteInto('rule_id=?', $ruleId), $write->quoteInto('product_id=?', $productId)));
     if (!$rule->getConditions()->validate($product)) {
         $write->delete($this->getTable('catalogrule/rule_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();
     $actionStop = $rule->getStopRulesProcessing();
     if ($this->getVersionHelper()->isGe1700()) {
         $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     }
     $rows = array();
     if (!$this->getVersionHelper()->isGe1600()) {
         $header = 'replace 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 ';
     }
     try {
         foreach ($websiteIds as $websiteId) {
             foreach ($helper->getStoreIdsByWebsiteId($websiteId) as $storeId) {
                 if (in_array($storeId, $storeIds)) {
                     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[] = "(\n                                    '{$ruleId}',\n                                    '{$fromTime}',\n                                    '{$toTime}',\n                                    '{$websiteId}',\n                                    '{$storeId}',\n                                    '{$customerGroupId}',\n                                    '{$productId}',\n                                    '{$actionOperator}',\n                                    '{$discountAmount}',\n                                    '{$actionStop}',\n                                    '{$sortOrder}'\n                                )";
                             if (sizeof($rows) == 100) {
                                 $sql = $header . join(',', $rows);
                                 $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 = $header . join(',', $rows);
                 $write->query($sql);
             }
         }
     } catch (Exception $e) {
         $write->rollback();
         throw $e;
     }
     $this->applyAllRulesForDateRange(null, null, $product);
     $write->commit();
     return $this;
 }