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?.
Contents
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.
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.
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?
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
View Comments
hii How Can Remove As per Catalog Rule Apply On Current Product When Product
Add Cart.
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]);
Thank You
Yes, You need to do the code according to your requirements.