【问题标题】:How do I mark products that match shopping cart price rules如何标记符合购物车价格规则的产品
【发布时间】:2014-02-13 14:56:09
【问题描述】:

我在我的自定义模块中使用购物车价格规则,一切正常我只想根据每个规则获取产品 ID。 如果购物车中有 10 件产品,其中 3 件应用了一些规则,则规则 #1 用于 2 个产品,规则 #2 用于 1 个产品。如何根据每条规则标记产品 ID?

【问题讨论】:

    标签: php magento magento-1.7 shopping-cart


    【解决方案1】:

    只需将 Validator.php 中的 process 函数更改为

    public function process($_quote) {
        $i               = 0;
        $quote           = $_quote;
        $customerSession = Mage::getSingleton('customer/session');
        foreach ($this->_rules as $rule) {
            // already tried to validate and failed
            if ($rule->getIsValid() === false) {
                continue;
            }
            if ($rule->getIsValid() !== true) { 
                $rule->afterLoad();
                if (!$rule->validate($quote)) { // quote does not meet rule's conditions , //Call Found.php
                    $rule->setIsValid(false);
                    continue;
                }
                $rule->setIsValid(true); // passed all validations, remember to be valid
            }
    
            $this->_appliedProductsIds[]                 =  Mage::getSingleton('checkout/session')->getReturnProductRuleValues();
            $this->_appliedProductsIds[$i]['program_id'] =  $rule->getProgramId();
            Mage::getSingleton('checkout/session')->unsReturnProductRuleValues($this->_ReturnValues);
            $i = $i + 1;
        }
        return $this;
    }
    

    validate函数在Found.php中

    public function validate(Varien_Object $object) { 
    //Called form Validator.php
        $all       = $this->getAggregator() === 'all';
        $true      = (bool)$this->getValue();
        $found     = false;
    
    $Count =  count($object->getAllItems()); 
    $i = 0;
        foreach ($object->getAllItems() as $item) {
            $found = $all ? true : false;
            foreach ($this->getConditions() as $cond) {
    
                $validated = $cond->validate($item); // Call to Product.php's function 'validate'
    
        if($validated) {
            $this->_ProductId[] = $item->getProductId();
        }
    
        if($i == $Count) {
            if ($all && !$validated) {
            $found = false;
            break;
            } elseif (!$all && $validated) {
            $found = true;
            break 2;
            }
        }
    
            }
        if($i == $Count) {
        if ($found && $true) {
            break;
        }
        }
        $i = $i + 1;
        }
    
    $this->_ReturnValues['Product_Id'] = $this->_ProductId;
    if(!empty($this->_ProductId)) {
        $this->_ReturnValues['Bool'] = true;
    } else {
        $this->_ReturnValues['Bool'] = false;
    }
    
        if ($found && $true) {
            // found an item and we're looking for existing one
        Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
            return true;
        } elseif (!$found && !$true) {
            // not found and we're making sure it doesn't exist
        Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
            return true;
        }
    Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
        return false;
    }
    

    并使用 Vadidator 类对象访问 Observer 中的值,例如

    $validator = Mage::getModel('modulename/validator')
                   ->init($customer->getWebsiteId(), $customerGroupId);
    
    $v                   = $validator->process($quote);
    $_appliedProductsIds = $v->_appliedProductsIds;
    

    希望这会对某人有所帮助..

    【讨论】:

      【解决方案2】:

      我认为以下功能会对您有所帮助:

      加载特定规则,然后使用以下函数获取受影响的产品:

      $rule->getMatchingProductIds()

      public function getSpecialPriceProducts()
      {
              $categoryID = $this->getCategoryId();
              if($categoryID)
              {
                $category = new Mage_Catalog_Model_Category();
                $category->load($categoryID); // this is category id
                $collection = $category->getProductCollection();
              } else
              {
                $collection = Mage::getResourceModel('catalog/product_collection');
              }
      
              $todayDate = date('m/d/y');
              $tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
              $tomorrowDate = date('m/d/y', $tomorrow);
      
              Mage::getModel('catalog/layer')->prepareProductCollection($collection);
              $collection->addAttributeToSort('created_at', 'desc');
              $collection->addStoreFilter()
                      ->addAttributeToSelect(array('name', 'price', 'short_description','image','small_image','url_key'), 'inner');
              $collection->addAttributeToFilter('special_price', array('gt' => 0));
              $collection->addAttributeToFilter('special_to_date', array('date' => true, 'to' => $todayDate))
                  ->addAttributeToFilter('special_from_date', array('or'=> array(
                  0 => array('date' => true, 'from' => $tomorrowDate),
                  1 => array('is' => new Zend_Db_Expr('null')))
                  ), 'left');
      
              $rules = Mage::getResourceModel('catalogrule/rule_collection')->load();
      
              // read: if there are active rules
              if($rules->getData()) {
                  $rule_ids = array(); // i used this down below to style the products according to which rule affected
                  $productIds[] = array(); // this will hold the ids of the products
      
                  foreach($rules as $rule) {
                      $rule_ids[] = $rule->getId();
                      $productIds = $rule->getMatchingProductIds(); // affected products come in here
                  }
      
                  // merge the collections: $arr is an array and keeps all product IDs we fetched before with the special-price-stuff
                  $arr = $collection->getAllIds();
                  if($productIds) {
                      // if there are products affected by catalog price rules, $arr now also keeps their IDs
                      $arr = array_merge($arr,$productIds);
                  }
      
                  // we initialize a new collection and filter solely by the product IDs we got before, read: select all products with their entity_id in $arr
                  $collection = Mage::getModel('catalog/product')->getCollection()
                      ->addAttributeToSelect(array('name', 'price', 'short_description','image','small_image','url_key'), 'inner')
                      ->addAttributeToFilter('entity_id',array('in'=>$arr))
                      ->load();
              }
              return $collection;
      }
      

      【讨论】:

      • $rule->getMatchingProductIds() 不是购物车价格规则的功能
      • 是CatalogRule模块中模型类Rule的函数:docs.magentocommerce.com/Mage_CatalogRule/…
      • 是的,但请仔细阅读问题,它是关于购物车价格规则的
      猜你喜欢
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2013-02-18
      相关资源
      最近更新 更多