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

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

17 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

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

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago