Search code examples
phpmagentozend-frameworkmagento-1.9magento-1.9.1

Magento - validate quote item against salesrule conditions


I would like to validate a quote item against the conditions of a salesrule/rule.

Actions work for validating against singular item with following:

$rule->getActions()->validate($item)

Is there a way to do this for the conditions?

Update: I have achieved this currently by creating a quote on the fly and only adding one quote item to it, then validating against that fake quote. However, I'd still like to know if there is another way to achieve this.


Solution

  • Working example:

    $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
    $quote = Mage::getSingleton('sales/quote')->load($quoteId);
    
    $fakeQuote = clone $quote;
    $fakeQuote->setId(null);
    
    $product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
    
    $item = Mage::getModel('sales/quote_item')->setQuote($fakeQuote)->setProduct($product);
    $item->setAllItems(array($product));
    $item->getProduct()->setProductId($product->getEntityId());
    $item->setQty(1);
    
    $item->getQuote()->setData('items_collection', array($item));
    
    $rule = Mage::getModel('salesrule/rule')->load(RULE_ID);
    if ($rule->getConditions()->validate($item)) {
        // Do something
    }