1

I have try with this code make tax rule programmatically. but it's not working infact model not loaded. you can see this code :

$objectManagers = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$fixtureTaxRule = $objectManagers->create('Magento\Tax\Model\Calculation\Rule');

i have get this error :

Class 'Magento\TestFramework\Helper\Bootstrap' not found

can you please suggest how can i create tax rule ?

4

Finally i have solve this issue. Now you can use this code and i hope it's also beneficial for you guys.

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $fixtureTaxRule = $objectManager->create(\Magento\Tax\Model\Calculation\Rule::class);
 $fixtureTaxRule->setCode("Test");
 $fixtureTaxRule->setPriority(0);
 $fixtureTaxRule->setCustomerTaxClassIds(array(3));
 $fixtureTaxRule->setProductTaxClassIds(array(2));
 $fixtureTaxRule->setTaxRateIds(array(3));
 $fixtureTaxRule->save();
1
  • Hi, Can you please share your whole code in your answer? – Kaushal Suthar Feb 10 '17 at 11:30
1

Using object manager is not allowed so answer above seems to be wrong.

Correct answer can be found in setup/src/Magento/Setup/Fixtures/TaxRulesFixture.php in setTaxMode method.

taxRateFactory and taxRateRepository together with taxRuleFactory and taxRuleRepository should be used.

    //Add Tax Rate for selected Tax Mode
    /** @var $taxRate TaxRateInterface */
    $taxRate = $this->taxRateFactory->create();
    $taxRate->setCode($taxMode)
        ->setRate(self::DEFAULT_TAX_RATE)
        ->setTaxCountryId(self::DEFAULT_TAX_COUNTRY)
        ->setTaxPostcode('*');

    $taxRateData = $this->taxRateRepository->save($taxRate);

    //Add Tax Rule for Tax Mode
    /** @var $taxRuleDataObject TaxRuleInterface */
    $taxRuleDataObject = $this->taxRuleFactory->create();
    $taxRuleDataObject->setCode($taxMode)
        ->setTaxRateIds([$taxRateData->getId()])
        ->setCustomerTaxClassIds([self::DEFAULT_CUSTOMER_TAX_CLASS_ID])
        ->setProductTaxClassIds([self::DEFAULT_PRODUCT_TAX_CLASS_ID])
        ->setPriority(0)
        ->setPosition(0);

    $this->taxRuleRepository->save($taxRuleDataObject);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.