Applying Discounts After Tax Calculation in Magento 2
In Magento 2, by default, taxes are calculated based on the item price after discounts have been applied. This standard behavior ensures tax calculations remain accurate and compliant with tax laws. However, there may be specific business scenarios where you need to apply a discount after the tax has been calculated.
To achieve this, we’ll need to override the core functionality of Magento’s order calculation process. Here’s a step-by-step guide on how to implement this:
Step 1: Enable Developer Mode and Compilation
Before making changes to the core files, ensure your Magento instance is in developer mode and compilation is disabled.
php bin/magento deploy:mode:set developer
n98-magerun2.0 sys:disable-compilation
Step 2: Create a Custom Discount Plugin
We’ll create a custom plugin to override the core tax calculation. Navigate to your Magento root directory and open app/code folder.
Create a new directory under app/code:
app/code/Vendor/CustomDiscount/etc
Within etc, add a di.xml file with the following content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Tax\Model\Sales\Total\Quote\Tax">
<plugin name="custom_discount_plugin" type="Vendor\CustomDiscount\Plugin\Tax"/>
</type>
</config>
Step 3: Implement the Plugin
Create a new directory under app/code/Vendor/CustomDiscount/Plugin:
app/code/Vendor/CustomDiscount/Plugin
Inside this folder, create a Tax.php file with the following PHP code:
namespace Vendor\CustomDiscount\Plugin;
use Magento\Tax\Model\Sales\Total\Quote\Tax as TaxModel;
use Magento\Framework\App\RequestInterface;
class Tax
{
public function afterCollect(
TaxModel $subject,
RequestInterface $request,
float $taxAmount = 0.0
): float {
// Perform your custom discount calculation here
// The $taxAmount variable holds the tax amount before applying discounts
// For demonstration purposes, let's apply a flat discount of 5%
$discount = 0.05 * $taxAmount;
return $taxAmount - $discount;
}
}
Step 4: Recompile and Clear Cache
After implementing your custom plugin, recompile the code and clear the cache:
php bin/magento setup:di:compile
n98-magerun2.0 sys:flush-cache
Step 5: Test Your Custom Discount Calculation
Create a new order programatically or manually in your Magento admin panel, and verify that the discount is applied after tax calculation.
Conclusion
In this blog post, we’ve demonstrated how to override the core tax calculation process in Magento 2 to apply discounts after taxes have been calculated. By following our step-by-step guide, you can achieve a custom tax workflow tailored to your business requirements.
Remember, modifying core files should always be done with caution and proper testing. It’s essential to create a backup of your site before making any changes.
For more advanced Magento 2 development tutorials and best practices, visit https://laravelcompany.com/magento-development.