Shopping Cart Price Rules – How to Check if Shopping Cart Price Rule Applied to Quote

coupon-codesprice-rulesshopping-cart-price-rules

I have a shopping cart rule setup which applies a discount if the product has a specific attribute – there's no coupon code involved.

Throughout the site I just need to check whether or not that shopping cart rule has been applied to the quote.

I've tried this:

$coupon_code = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();

But that doesn't work; it only works with coupon codes input by the user.

How would I check if such a shopping cart rule has been applied?

Best Answer

$appliedRuleIds = Mage::getSingleton('checkout/session')->getQuote()->getAppliedRuleIds();

This will give you the ids of the rules applied to the quote separated by comma.
you can turn them into an array like this:

$appliedRuleIds = explode(',', $appliedRuleIds);

if you want to get the rules applied as objects you can do this:

//$rules = Mage::getModel('salesrule/rule')->getCollection()->addFieldToFilter('rule_id' => array('in' => $appliedRuleIds));
$rules =  Mage::getModel('salesrule/rule')->getCollection()->addFieldToFilter('rule_id' , array('in' => $appliedRuleIds));

foreach ($rules as $rule) {
    //do something with $rule
}
Related Topic