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 imported order have not discount but according to Magento some cart rules applied that order then discount automatically applied on that order.
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.
For creating order programatically you can read Create Quote And Order Programmatically In Magento2. It will help you to create order in Magento2.I am explaining here only remove cart rules related files.
Please open this file Magento\SalesRule\Model\RulesApplier and check the function applyRules 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.
For changing the return value of function we will use after plugin here.You can read Magento2 – Create and Use Plugins for learn about plugin in more detail.
1 – Create di.xml for define plugin Webkul/RemoveCartRule/etc/di.xml
<?xml version="1.0"?>
<!--
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_RemoveCartRule
* @author Webkul
* @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
-->
<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_discount_on_order" type="Webkul\RemoveCartRule\Plugin\SalesRule\Model\RulesApplier" sortOrder="1" disabled="false"/>
</type>
</config>
2 – Create plugin file Webkul/RemoveCartRule/Plugin/SalesRule/Model/RulesApplier.php
<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_RemoveCartRule
* @author Webkul
* @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/
namespace Webkul\RemoveCartRule\Plugin\SalesRule\Model;
use Magento\Framework\Session\SessionManager;
class RulesApplier
{
/**
* @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection
*/
private $rules;
/**
* @param \Magento\SalesRule\Model\ResourceModel\Rule\Collection $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"=>0]);
$result = $proceed($item, $rules, $skipValidation, $couponCode);
return $result;
}
}
After add this file run following command.
php bin/magento setup:di:compile
Please try this code.If you have any doubts about the above explanation please ask in the comments
Be the first to comment.