How to override Magento\SalesRule\Model\RulesApplier class in custom module ?
i have tried
etc/di.xml
<preference for="Magento\SalesRule\Model\RulesApplier" type="Myvendor\SalesRule\Model\RulesApplier" />
Myvendor/SalesRule/Model/RulesApplier.php <?php
namespace Myvendor\SalesRule\Model;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
use Magento\Framework\App\ObjectManager;
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
use Magento\SalesRule\Api\Data\RuleDiscountInterfaceFactory;
use Magento\SalesRule\Api\Data\DiscountDataInterfaceFactory;
use Magento\GiftCard\Model\Catalog\Product\Type\Giftcard as GiftCardType;
/**
* Rule applier model
*/
class RulesApplier extends \Magento\SalesRule\Model\RulesApplier
{
/**
* Application Event Dispatcher
*
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $_eventManager;
/**
* @var \Magento\SalesRule\Model\Utility
*/
protected $validatorUtility;
/**
* @var ChildrenValidationLocator
*/
private $childrenValidationLocator;
/**
* @var CalculatorFactory
*/
private $calculatorFactory;
/**
* @var \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory
*/
protected $discountFactory;
/**
* @var RuleDiscountInterfaceFactory
*/
private $discountInterfaceFactory;
/**
* @var DiscountDataInterfaceFactory
*/
private $discountDataInterfaceFactory;
/**
* @var array
*/
private $discountAggregator;
/**
* RulesApplier constructor.
* @param CalculatorFactory $calculatorFactory
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param Utility $utility
* @param ChildrenValidationLocator|null $childrenValidationLocator
* @param DataFactory|null $discountDataFactory
* @param RuleDiscountInterfaceFactory|null $discountInterfaceFactory
* @param DiscountDataInterfaceFactory|null $discountDataInterfaceFactory
*/
public function __construct(
\Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory $calculatorFactory,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\SalesRule\Model\Utility $utility,
ChildrenValidationLocator $childrenValidationLocator = null,
DataFactory $discountDataFactory = null,
RuleDiscountInterfaceFactory $discountInterfaceFactory = null,
DiscountDataInterfaceFactory $discountDataInterfaceFactory = null
) {
$this->calculatorFactory = $calculatorFactory;
$this->validatorUtility = $utility;
$this->_eventManager = $eventManager;
$this->childrenValidationLocator = $childrenValidationLocator
?: ObjectManager::getInstance()->get(ChildrenValidationLocator::class);
$this->discountFactory = $discountDataFactory ?: ObjectManager::getInstance()->get(DataFactory::class);
$this->discountInterfaceFactory = $discountInterfaceFactory
?: ObjectManager::getInstance()->get(RuleDiscountInterfaceFactory::class);
$this->discountDataInterfaceFactory = $discountDataInterfaceFactory
?: ObjectManager::getInstance()->get(DiscountDataInterfaceFactory::class);
}
/**
* Apply rules to current order item
* Applied fix to prevent cart rule apply to gc product
* @param AbstractItem $item
* @param Collection $rules
* @param bool $skipValidation
* @param mixed $couponCode
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function applyRules($item, $rules, $skipValidation, $couponCode)
{
$address = $item->getAddress();
$appliedRuleIds = [];
$this->discountAggregator = [];
/* @var $rule Rule */
foreach ($rules as $rule) {
if (!$this->validatorUtility->canProcessRule($rule, $address)) {
continue;
}
if (!$skipValidation && !$rule->getActions()->validate($item)) {
if (!$this->childrenValidationLocator->isChildrenValidationRequired($item)) {
continue;
}
$childItems = $item->getChildren();
$isContinue = true;
if (!empty($childItems)) {
foreach ($childItems as $childItem) {
if ($rule->getActions()->validate($childItem)) {
$isContinue = false;
}
}
}
if ($isContinue) {
continue;
}
}
$product = $item->getProductType();
if ($product != GiftCardType::TYPE_GIFTCARD) {
$this->applyRule($item, $rule, $address, $couponCode);
}
$appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
if ($rule->getStopRulesProcessing()) {
break;
}
}
return $appliedRuleIds;
}
}
This works but then "ADD to Cart" not working. no logs no error.