top of page

Add custom discount before tax calculation in Magento 2

Suppose we are creating a custom discount and want to add it before the tax calculation. So what should we do now?


Actually taxes are calculated item wise in Magento 2, so we need to set the applicable discount amount to the cart items just before the tax calculation to accomplish our task.


So for that we need to create a total collector class and set its sort order just before the tax calculation. Look at the following snippets -


Vishesh\CustomDiscount\etc\sales.xml


<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>
</config>

Here we are using sort order 445 as the sort order of the tax total collector class is 450.


Vishesh\CustomDiscount\Model\Total\Discount

Our task done, now our discount is applied before the tax calculation.


Thank you. Happy coding :).

1 Comment


Guest
Jun 13, 2024

Not Working

Like

Subscribe Form

Thanks for submitting!

©2022 by Vishesh Dwivedi.

bottom of page