{"id":147612,"date":"2018-10-13T16:47:38","date_gmt":"2018-10-13T16:47:38","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=147612"},"modified":"2024-03-26T07:50:11","modified_gmt":"2024-03-26T07:50:11","slug":"how-to-remove-applied-cart-rules-programatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/","title":{"rendered":"How to remove applied cart rules programatically in Magento 2"},"content":{"rendered":"\n<p>In this blog we will learn about how to remove <a href=\"https:\/\/store.webkul.com\/magento2-special-promotions.html\">cart rules<\/a> during place order programatically in Magento2.<\/p>\n\n\n\n<p>Sometimes we need to create order in Magento store from other channels programatically. If\u00a0 imported order have not discount but according to Magento some cart rules applied that order then discount automatically applied on that order.<\/p>\n\n\n\n<p>Due to this some difference occur between grand total of channel order and Magento order. For overcome this problem you can use this concept to remove cart rules from order.<\/p>\n\n\n\n<p>For creating order programatically you can read <a href=\"https:\/\/webkul.com\/blog\/create-quote-and-order-programmatically-in-magento2\/\">Create Quote And Order Programmatically In Magento2<\/a>.&nbsp; It will help you to create order in Magento2.I am explaining here only remove cart rules related files.<\/p>\n\n\n\n<p>Please open this file Magento\\SalesRule\\Model\\RulesApplier and check the function applyRules&nbsp; which is return array of applied cart rules ids if any apply on that order.So if we will return only blank array then apply cart rules.<\/p>\n\n\n\n<p>For changing the return value of function we will use<strong> after plugin<\/strong> here.You can read<strong>&nbsp;&nbsp;<\/strong><strong><a href=\"https:\/\/webkul.com\/blog\/magento2-use-plugins\/\">Magento2 \u2013 Create and Use Plugins<\/a>&nbsp;<\/strong>for learn about plugin in more detail.<\/p>\n\n\n\n<p>1 &#8211; Create di.xml for define plugin\u00a0Webkul\/RemoveCartRule\/etc\/di.xml<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n* Webkul Software.\n*\n* @category  Webkul\n* @package   Webkul_RemoveCartRule\n* @author    Webkul\n* @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n* @license   https:\/\/store.webkul.com\/license.html\n*\/\n--&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Module\/etc\/module.xsd&quot;&gt;\n    &lt;type name=&quot;Magento\\SalesRule\\Model\\RulesApplier&quot;&gt;\n        &lt;plugin name=&quot;remove_discount_on_order&quot; type=&quot;Webkul\\RemoveCartRule\\Plugin\\SalesRule\\Model\\RulesApplier&quot; sortOrder=&quot;1&quot; disabled=&quot;false&quot;\/&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p>2 &#8211; Create plugin file\u00a0Webkul\/RemoveCartRule\/Plugin\/SalesRule\/Model\/RulesApplier.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * @category  Webkul\n * @package   Webkul_RemoveCartRule\n * @author    Webkul\n * @copyright Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html\n *\/\n\nnamespace Webkul\\RemoveCartRule\\Plugin\\SalesRule\\Model;\n\nuse Magento\\Framework\\Session\\SessionManager;\n\nclass RulesApplier\n{\n    \/**\n     * @var \\Magento\\SalesRule\\Model\\ResourceModel\\Rule\\Collection\n     *\/\n    private $rules;\n\n    \/**\n     * @param \\Magento\\SalesRule\\Model\\ResourceModel\\Rule\\Collection $rules\n     *\/\n    public function __construct(\n        \\Magento\\SalesRule\\Model\\ResourceModel\\Rule\\CollectionFactory $rulesFactory\n    ) {\n        $this-&gt;ruleCollection = $rulesFactory;\n    }\n\n    public function aroundApplyRules(\n        \\Magento\\SalesRule\\Model\\RulesApplier $subject,\n        \\Closure $proceed,\n        $item,\n        $rules,\n        $skipValidation,\n        $couponCode\n    ) {\n        $rules = $this-&gt;ruleCollection-&gt;create()-&gt;addFieldToFilter(&quot;rule_id&quot;, &#091;&quot;eq&quot;=&gt;0]);\n        $result = $proceed($item, $rules, $skipValidation, $couponCode);\n        return $result;\n    }\n}<\/pre>\n\n\n\n<p>After add this file run following command.<\/p>\n\n\n\n<p>php bin\/magento setup:di:compile<\/p>\n\n\n\n<p>Please try this code.If you have any doubts about the above explanation please ask in the comments<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically. If\u00a0 imported order have not discount but according to Magento some cart rules applied that order then discount automatically applied on that order. Due <a href=\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302],"tags":[7666,2878,2070,7665],"class_list":["post-147612","post","type-post","status-publish","format-standard","hentry","category-magento2","tag-create-order-programatically","tag-create-plugin","tag-magento2","tag-remove-apply-cart-rule"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to remove applied cart rules programatically in Magento 2<\/title>\n<meta name=\"description\" content=\"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to remove applied cart rules programatically in Magento 2\" \/>\n<meta property=\"og:description\" content=\"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-13T16:47:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-26T07:50:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shubham Sharma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Sharma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\"},\"author\":{\"name\":\"Shubham Sharma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd\"},\"headline\":\"How to remove applied cart rules programatically in Magento 2\",\"datePublished\":\"2018-10-13T16:47:38+00:00\",\"dateModified\":\"2024-03-26T07:50:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\"},\"wordCount\":253,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"create order programatically\",\"Create Plugin\",\"Magento2\",\"remove apply cart rule\"],\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\",\"name\":\"How to remove applied cart rules programatically in Magento 2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-10-13T16:47:38+00:00\",\"dateModified\":\"2024-03-26T07:50:11+00:00\",\"description\":\"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to remove applied cart rules programatically in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd\",\"name\":\"Shubham Sharma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Shubham Sharma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/shubham-sharma967\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to remove applied cart rules programatically in Magento 2","description":"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"How to remove applied cart rules programatically in Magento 2","og_description":"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.","og_url":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-10-13T16:47:38+00:00","article_modified_time":"2024-03-26T07:50:11+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Shubham Sharma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Shubham Sharma","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/"},"author":{"name":"Shubham Sharma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd"},"headline":"How to remove applied cart rules programatically in Magento 2","datePublished":"2018-10-13T16:47:38+00:00","dateModified":"2024-03-26T07:50:11+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/"},"wordCount":253,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["create order programatically","Create Plugin","Magento2","remove apply cart rule"],"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/","name":"How to remove applied cart rules programatically in Magento 2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-10-13T16:47:38+00:00","dateModified":"2024-03-26T07:50:11+00:00","description":"In this blog we will learn about how to remove cart rules during place order programatically in Magento2. Sometimes we need to create order in Magento store from other channels programatically.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-remove-applied-cart-rules-programatically-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to remove applied cart rules programatically in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd","name":"Shubham Sharma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Shubham Sharma"},"url":"https:\/\/webkul.com\/blog\/author\/shubham-sharma967\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=147612"}],"version-history":[{"count":2,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147612\/revisions"}],"predecessor-version":[{"id":429466,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/147612\/revisions\/429466"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=147612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=147612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=147612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}