How to Remove Applied Cart Rules in Magento 2

How to Remove Applied Cart Rules in Magento 2

Howdy Magento Folks?,

Welcome to our Magento 2 How to Blog Series. What are we going to do today? Today we are going to learn How To Remove Applied Cart Rules in Magento 2 programmatically. In case you haven’t caught our latest published article on How To Add JS File in Frontend for All Pages in Magento 2. Let’s start learning?.

Introduction:

Basically, when there is a scenario to create order programmatically in your Magento 2 store then there are many possibilities. Sometimes the imported order discount is not applied but as stated in Magento cart rules the discount must be applied. To overcome this situation admin need to just apply some cart rules for that specific cart order and after that, the discount will be applied successfully. Many times because of some difference between the total at checkout the problems are faced and to overcome this situation go through the easy solution given below.

If you are in need of creating order programmatically then look at this article How to Create Order Programmatically in Magento 2. Build your advanced checkout page now with the help of MageComp’s Magento 2 Checkout Success Page extension.

Steps to Remove Programmatically Applied Cart Rules in Magento 2:

Step 1: Firstly, you need to add di.xml in the following path.

app\code\Vendor\Extension\etc\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <type name="Magento\SalesRule\Model\RulesApplier">
        <plugin name="remove_cart_rule" type="Vendor\Extension\Plugin\SalesRule\Model\RulesApplier"/>
    </type>
</config>

Step 2: Now, add RulesApplier.php file in following path:

app\code\Vendor\Extension\Plugin\SalesRule\Model\RulesApplier.php

<?php

namespace Vendor\Extension\Plugin\SalesRule\Model;

class RulesApplier
{

    private $rules;

    public function __construct(
        \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $rulesFactory
    ) {
        $this->ruleCollection = $rulesFactory;
    }

    public function aroundApplyRules(
        \Magento\SalesRule\Model\RulesApplier $subject,
        \Closure $proceed,
        $item,
        $rules,
        $skipValidation,
        $couponCode
    ) {
        $rules = $this->ruleCollection->create()->addFieldToFilter("rule_id", ["eq"=> ""]);
        $result = $proceed($item, $rules, $skipValidation, $couponCode);
        return $result;
    }
}

That’s it you will be easily able to Remove Applied Cart Rules.

Final Words:

I would expect the solution given in the above solution was helpful for all and with that let’s conclude our blog. If you find any concerns regarding this article then do connect with our Support Team. But in the meantime, share the article with your Magento pals for helping them to get out of this problem. As always suggestions are always welcomed at MageComp write down in the comment section below. 

Happy Coding?

Previous Article

How to Enable Persistent Shopping Cart in Magento 2

Next Article

How To Hide Add To Cart Button in Magento 2

Write a Comment
  1. If you want to disable multiple rules you can do
    $ruleIds = array(1, 5, 9); //Rule Ids to remove
    $rules = $this->ruleCollection->create()->addFieldToFilter(‘rule_id’, [‘in’ => $ruleIds]);

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨