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:


	
	
	
    MyCompany_TaxCustomizer
    1.0.0
    Custom Tax Calculation 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:


	
	
    
        
            
                
            
        
    
    
     
           
           MyCompany_TaxCustomizer_Model_Tax_Calculator
           
     
    
	
	

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.