0

How to get applied cart price rule in magento add to cart observer ?

  • Which observer event are you using.? – Nagaraj Juro Nov 16 '18 at 7:13
  • @NagarajJuro i am using checkout_cart_product_add_after observer – Ansar Husain Nov 16 '18 at 8:22
0

Try to getting the appliedRuleIds by calling the $quote from your $quoteItem

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer instance="Vendor\Module\Observer\ProcessAppliedRules" name="Vendor_Module::process_applied_rules"/>
    </event>
</config>

ProcessAppliedRules.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ProcessAppliedRules implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        /** @var $quoteItem \Magento\Quote\Model\Quote\Item */
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $quote = $quoteItem->getQuote();

        $appliedRuleIds = $quote->getAppliedRuleIds();

        // Do some crazy stuff here

        return $this;
    }
}
0

As the OP has asked in Magento 1 the login is similar as @Tuyen has posted the only thing I would like to add is you get the applied rule ids for each item using

$quoteItem = $observer->getEvent()->getQuoteItem();
$appliedRuleIdsOfItems = $quoteItem->getAppliedRuleIds();

and if you need for full quote use

$quote = $observer->getEvent()->getQuoteItem()->getQuote();
$appliedRuleIds = $quote->getAppliedRuleIds();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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