Categories: How ToMagento 2

How to Create Tax Rules Programmatically in Magento 2

Creating tax rules in Magento 2 controls the tax amount customers will pay while ordering products from your store. This tax is calculated based on product class, customer class, tax rates and shipping address of the customers. Whenever a user add products to cart, tax is calculated analyzing the shopping cart. While developing an extension or working for some of our clients, we require to create tax rules programmatically in Magento 2 where whenever the extension is installed, all the rules created automatically.

If you sometimes require to create tax rules programmatically, here’s the detailed code:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
 
  

 $taxCalculationRate1 = $objectManager->create('Magento\Tax\Model\Calculation\Rate');//->load(1);
 $taxCalculationRate1->setCode("CODE");
 $taxCalculationRate1->setTaxCountryId("IN");
 $taxCalculationRate1->setTaxRegionId("REGION");
 $taxCalculationRate1->setZipIsRange("0");
 $taxCalculationRate1->setTaxPostcode("*");
  $taxCalculationRate1->setRate("2.5");
 $taxCalculationRate1->save(); 
 

 $fixtureTaxRule1 = $objectManager->create('Magento\Tax\Model\Calculation\Rule');//->load(1);
 $fixtureTaxRule1->setCode("CODE");
 $fixtureTaxRule1->setPriority(0);
 $fixtureTaxRule1->setCustomerTaxClassIds(array(3));
 $fixtureTaxRule1->setProductTaxClassIds(array(8));
 $fixtureTaxRule1->setTaxRateIds(array($taxCalculationRate1->getId()));
 $fixtureTaxRule1->save();
 
 echo $fixtureTaxRule1->getId();

Hope the guide has enough helped you to create tax rules programmatically. Still if you stuck somewhere or have any questions regarding, feel free to ask through commenting.

Click to rate this post!
[Total: 8 Average: 2.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

12 hours ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

1 day ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

3 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

5 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

6 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

6 days ago