How To

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?

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago