6

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?

2 Answers 2

26
$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
}
4
  • hi marius, i have to apply similar solution. but getAppliedRuleIds stores both catalog price rule and shopping cart price rule. Now if the field value is only 6 (which is catalog price rule) and a shopping cart price rule too exists in store with id 6. in this case, with your solution, how can we define which one was applied ? shopping cart price rule or catalog price rule ? any help is appreciated. Commented Jun 26, 2015 at 14:24
  • @ReenaParekh. I'm almost certain the getAppliedRuleIds does not return the catalog price rules applied to the quote/order. Only shopping cart price rules that are applied.
    – Marius
    Commented Jun 26, 2015 at 14:27
  • Hi @Marius , I too need to get the same thing what you have mentioned in above code in magento2.2.2. So Please tell me how do I solve the same issue in magento 2
    – Jaisa
    Commented Feb 28, 2018 at 7:28
  • Hi @Marius How can we get discount amount from quote based on rule id? Commented Mar 2, 2019 at 6:41
0

For Magento 2

Try This Code

protected $quoteRepository;
protected $ruleData;

public function __construct(
    ...................................................................
    \VendoreName\ModuleName\Model\CustomRuleDataFactory $quoteRepository,
    \Magento\Quote\Api\CartRepositoryInterface $ruleData,
    ...................................................................
) {
    ...................................................................
    $this->quoteRepository = $quoteRepository;
    $this->ruleData = $ruleData;
    ...................................................................
}

public function execute()
{
    $address = 0;
    $quotesVal = 15
    $quotesData = $this->quoteRepository->get($quotesVal);
    $customerId = (int) $quotesData->getCustomerId();
    $productCount = 0;
    foreach ($quotesData->getAllItems() as $quotesDataKey => $quotesDataVal) {
        $address = $quotesDataVal->getAddress();
        $productCount++;
        break;
    }
    if ($productCount) {
        $address->setCachedItemsAll($quotesData->getAllItems());
        $cartRuleData = $this->ruleData->create()->getCollection();
        foreach ($cartRuleData as $ruleKey => $ruleVal) {
            if ($ruleVal->getConditions()->validate($address)){ // if ($ruleVal->getActions()->validate($address))
                echo "Rule Applied";
            }
        }
    }
}

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.