2

I would like to modify Discount Amount of cart price rule on the cart page. Actually Magento apply rule using class Magento\SalesRule\Model\RulesApplier and function is protected function setDiscountData($discountData, $item)

So for that, I create a preference to modify Discount Amount

<preference for="Magento\SalesRule\Model\RulesApplier" type="Namespace\Modulename\Model\RulesApplier" />

And my logic like below :

<?php
namespace Namespace\Modulename\Model;

use Namespace\Modulename\Model\Source\ShowCoupon;
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
use Namespace\Modulename\Model\Source\PromotionType;

class RulesApplier extends \Magento\SalesRule\Model\RulesApplier
{
    /**
     * @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection
     */
    protected $rules;

    /**
     * @var \Magento\SalesRule\Model\Utility
     */
    protected $validatorUtility;

    public function __construct(
        CalculatorFactory $calculatorFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\SalesRule\Model\Utility $utility,
        \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $rulesFactory,
        ChildrenValidationLocator $childrenValidationLocator = null
    ){
        $this->ruleCollection = $rulesFactory;
        parent::__construct($calculatorFactory, $eventManager, $utility, $childrenValidationLocator);
    }

    protected function setDiscountData($discountData, $item)
    {
        $item->setDiscountAmount(0);
        $item->setBaseDiscountAmount(0);
        $item->setOriginalDiscountAmount(0);
        $item->setBaseOriginalDiscountAmount(0);

        $quote = $item->getQuote();
        $quote->setCustomAmount($discountData->getAmount());
        $quote->setBaseCustomAmount($discountData->getBaseAmount());
        //echo '<pre>';print_r($quote->debug());die;
        return $this;
    }
}

In the above code, I am trying to set discount amount to zero and I want to set that discount amount to my custom fee value. but unfortunately, it is not working.

So I debug that and found that if you set $item->setDiscountAmount(0); to zero then the value is not set to your custom fee. so can someone help me to solve out this issue? what's I am doing wrong or is there any other method available?

In short, I would like to set discount amount to may custom fee and default discount amount($item->setDiscountAmount(0)) to zero at the same time same file

Any help would be appreciated! Thanks.

1 Answer 1

0

You can set the discount amount as below :

1.Create a sales.xml file in [vendor-name]/[module-name]/etc/sales.xml with

 <?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
    <section name="quote">
      <group name="totals">
        <item name="[custom-name]" instance="[vendor-name]\[module-name]\Model\Total\CartUpdate" sort_order="500"/>
      </group>  
    </section>
 </config> 

2.Create a file CartUpdate.php in [vendor-name]/[module-name]/model/Total with

  <?php
  namespace [vendor-name]\[module-name]\Model\Total;

  class CartUpdate extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
  {
      protected $priceCurrency;

      public function __construct(
      \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     )
     {
          $this->priceCurrency = $priceCurrency;
     }

     public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
     ) 
     {
         parent::collect($quote, $shippingAssignment, $total);     

         $customDiscount = 10;
         $discount =  $this->priceCurrency->convert($customDiscount);
         $total->setTotalAmount('discount', -$discount);
         $total->setBaseTotalAmount('discount', -$customDiscount);
         $total->setBaseGrandTotal($total->getBaseGrandTotal() - $customDiscount);
         $quote->setDiscount(-$discount);
         return $this;
      } 
 }

The result will be:

enter image description here

Here I set the discount amount as 10.You can set discount amount as 0 or above. If you want to set a discount as 0 then it will not be shown in frontend.

Hope this will be help you.....

2
  • this is not what I want, I want to get the discount amount and set that discount amount to my custom fee value. once the amount is set then remove actual discount amount. Commented Jul 10, 2019 at 4:26
  • Ok. I will update the answer soon.
    – Moni Raj
    Commented Jul 11, 2019 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.