Magento:我如何标记符合购物车价格规则的产品?

时间:2014-01-23 07:18:06

标签: php magento magento-1.7 shopping-cart

我在我的自定义模块中使用购物车价格规则,每件事情都很好我只想获得针对每条规则的产品ID。 如果购物车中有10种产品,其中3种产品适用一些规则,则2种产品的规则为#1,1种产品的规则为#2。如何根据每条规则标记产品ID?

2 个答案:

答案 0 :(得分:2)

只需将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;

跳这将有助于某人..

答案 1 :(得分:-2)

我认为以下功能可以帮助您:

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

$基于规则> 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;
}