How To Check Which Shopping Cart Rule Is Applied To Cart In Magento 2

Discount marketing can be profitable for your business when done well. Store owners add shopping cart price rules in Magento 2 to take advantage of the potential to grow revenues.

Sometimes, extending the strategy from the site wide discount, you may want to try your hands on providing the additional benefits based on the discount the customers have used. For example, offer free shipping to the customers having purchased more than 5 quantities with 5% discount.

To apply the above scenario in the store, the admin must check which shopping cart rule is applied to cart in Magento 2 prior to offering the additional benefits.

Earlier I posted the solution to check which shopping cart rule is applied in Magento. Today, I’ve come up with the same solution for Magento 2.

Method to Check Which Shopping Cart Rule is Applied to Cart in Magento 2

Add the below code.

use Magento\Checkout\Model\Session;
 
protected $checkoutSession;
 
public function __construct(
    Session $checkoutSession)
{
    $this->checkoutSession = $checkoutSession;
}
 
public function getAppliedRuleIds()
{
    $quote = $this->checkoutSession->getQuote();
    $appliedRuleIds = $quote->getAppliedRuleIds();
    $appliedRuleIds = explode(',', $appliedRuleIds);
    return $appliedRuleIds;
}

That’s it.

Do not forget to share this post with Magento Community via social media,.

Thank you.

Jignesh Parmar

Article by

Jignesh Parmar

An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.