CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to customize Magento's tax calculations

Step 1: Create a custom module

Create a new module in Magento by creating a directory MyCompany/TaxCustomizer in the app/code/local directory. Inside this directory, create a file etc/module.xml with the following content:


	<?xml version="1.0"?>
	<module>
		<name>MyCompany_TaxCustomizer</name>
		<version>1.0.0</version>
		<description>Custom Tax Calculation Module</description>
	</module>
	

Step 2: Create a custom tax class

Create a new file Model/Tax/Calculator.php in the same directory:


	

namespace MyCompany\TaxCustomizer\Model\Tax;

class Calculator extends Mage_Tax_Model_Calculator
{
    public function calculateTaxAmount(Varien_Object $item, $price, $rate)
    {
        // Check if the customer is from USA
        if ($item->getCustomer()->getCountryId() == 'US') {
            // Apply our custom tax rate of 10%
            return $price * $rate * 0.10;
        }
        return parent::calculateTaxAmount($item, $price, $rate);
    }
}
	

Step 3: Register our custom tax class

Create a file etc/config.xml in the same directory:


	<?xml version="1.0"?>
	<config>
		<modules>
			<MyCompany_TaxCustomizer>
				<depends>
					<Mage_Tax />
				</depends>
			</MyCompany_TaxCustomizer>
		</modules>
		<global>
			<models>
				<tax_calculator>
					<class>MyCompany_TaxCustomizer_Model_Tax_Calculator</class>
				</tax_calculator>
			</models>
		</global>
	</config>
	

Step 4: Clear cache and test

Clear the Magento cache using the command php bin/magento cache:clean and then refresh your browser cache.

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.