0

I am using event checkout_cart_add_product_complete which is triggered after Doing Add to cart any product, in the observer of this event I want to get associated cart price rule if any applied?

1 Answer 1

2

This will give you the ids of the rules applied to the quote separated by comma.

    public function __construct(
                                 ----------
                                \Magento\Checkout\Model\Session $checkoutSession,
                                \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepositoryInterface,
                                 ----------
                               ) {
                                 $this->checkoutSession = $checkoutSession;
                                 $this->ruleRepositoryInterface = $ruleRepositoryInterface;
                               }



    $appliedRuleIds = $this->checkoutSession->getQuote()->getAppliedRuleIds();

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

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

foreach ($appliedRuleIds as $appliedRuleId) {
    $this->ruleRepositoryInterface->getById($appliedRuleId);
}
4
  • Thanks a lot ,it works perfectly for my Question,another things i want to know that ,suppose a scenario i defined for a cart price rule buy X product and get Y Product at some discount , suppose customer added X product to cart how can we notify him about ** Y product ** Feb 29, 2020 at 10:51
  • welcome :). You can show some message/note at cart page based on the product in the cart. Feb 29, 2020 at 12:02
  • sure ,i got your point but which filter have to be applied for this X product for getting it associated gift (Discounted Y product) , Feb 29, 2020 at 12:33
  • Can you have any idear about this question magento.stackexchange.com/questions/305896/… Mar 2, 2020 at 16:13

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.