2

So I have a custom discount applied the following way:

on vendor\module\etc\sales.xml

<?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="testdiscount" instance="vendor\module\Model\Quote\Discount" sort_order="500"/>
        </group>
    </section>
</config>

On my vendor\module\Model\Quote\Discount.php

class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\SalesRule\Model\Validator $validator,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    ) 
    {
        $this->setCode('testdiscount');
        $this->eventManager  = $eventManager;
        $this->calculator    = $validator;
        $this->storeManager  = $storeManager;
        $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);

    $totalDiscount = //Get discount here
    if($totalDiscount > 0)
    {  
        $total->setDiscountAmount(          - $totalDiscount);
        $total->setBaseDiscountAmount(      - $totalDiscount);
        $total->setSubtotalWithDiscount($total->getSubtotal()         - $totalDiscount);
        $total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() - $totalDiscount);

        $total->addTotalAmount($this->getCode(),     - $totalDiscount);
        $total->addBaseTotalAmount($this->getCode(), - $totalDiscount);

    }

    return $this;

    }
}

My problem is that the taxes are being calculated using the total cart value without the discount applied. Any help on how to make the tax prices being applied to the total price including the discount?

2 Answers 2

2

The problem was I was only setting the discount for the totals and not for every item in the order:

foreach ($quote->getAllVisibleItems() as $item) 
{
   $item->setDiscountAmount(       $discount);
   $item->setBaseDiscountAmount(   $basedicount);
   $item->setDiscountPercent(      $percDiscount);
}
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_discount" instance="Vishesh\CustomDiscount\Model\Total\Discount" sort_order="445"/>
    </group>
</section>
Here we are using sort order **445** as the sort order of the tax total collector class is 450.

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.