13

Is there an event or place I can plugin for when a shopping cart price rule is no longer valid (such as when it expires or if you change the quantity of other products where it no longer applies)? I know that it is automatically removed from the cart, but I can't seem to find the place this is happening in the code.

I am writing a module that adds a free product to the cart for a coupon where the subtotal is more than X amount. It is all working except for when reducing the quantity of items in the cart where the subtotal goes below X. The coupon is getting removed, but my product is staying in the cart.

So far I've tapped into the following events to do what I need so far...

salesrule_validator_process
checkout_cart_update_items_after
sales_quote_collect_totals_before

I did find Magento\Quote\Model\CouponManagement which seemed promising since it has set() and remove() methods, but they don't appear to be getting called when the cart is updated.

In the checkout_cart_update_items_after it also seems like a logical place I can check, but it appears the rule is still set according to $cart->getQuote()->getAppliedRuleIds()


UPDATE: I ended up adding an observer for the checkout_cart_save_after event which runs after collectTotals() so knows if the rule is still on the cart or not. Then I basically compare $quote->getAppliedRuleIds() with $quote->getOrigData('applied_rule_ids') to see if the rule has been removed. I feel like there is a better way to do this, but it works for the moment. I still have the issue with coupons that expire though...

  • can you please explain in short, its very complex – Anas Mansuri Jul 5 at 7:03
0

I would do it with an around plugin for

\Magento\SalesRule\Model\RulesApplier::applyRules

There you have $rules parameter which would be an array containing old applied rule ids, and result of $proceed() call will be an array of applied ids. Maybe a better place could be found if you disclose what you intend to do with it.

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.