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.
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…