1

I am looking to get the tax rate value from a product (not quote item) in a plugin, which fires on the beforeAddProduct() method.

Currently I have the following in my plugin code:

$taxClassId = $product->getTaxClassId();

this returns an id such as 2 for example.

I attempt to use this value using the Magento\Tax\Api\TaxCalculationInterface getCalculatedRate() method:

$productTaxRate = $this->taxCalculation->getCalculatedRate($taxClassid);

but then this just returns a 0 which is incorrect as in the admin, the Tax Class is set to A_GEN_STANDARD which is set to a rate of 20%

Surely there must be a simple to get the tax rate value from a product?

Just to confirm, this is for a product, not a quote item :)

Thanks everyone

3 Answers 3

1
public function __construct(        
    \Magento\Tax\Model\Calculation $taxCalculation,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->taxCalculation = $taxCalculation;
    $this->_storeManager = $storeManager;        
} 
  Public function getTaxPercentage(){
      $productTaxClassId = $product->getTaxClassId();
      $defaultCustomerTaxClassId = $this->scopeConfig->getValue('tax/classes/default_customer_tax_class');
      $store      = $this->_storeManager->getStore(); 
      $request = $this->taxCalculation->getRateRequest(null, null, null, $store);
      $taxRate = $this->taxCalculation->getRate($request->setProductClassId($productTaxClassId))
}
1
  • Hi @quickshiftin. The problem with this approach, is it's getting the Tax rate from the store config, but what I need is to get the tax rate per product, specified in the Tax Class product attribute
    – SNAFU
    Commented Jan 20, 2022 at 10:15
0

You can get tax rate by:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$taxCalculation = $objectManager->create( 'Magento\Tax\Model\Calculation\Rate' )->load( <tax_rate_id> , 'tax_calculation_rate_id' );

$rate = $taxCalculation->getRate();
1
  • Hi @Muhammad. I'd like to avoid using the load() method where possible, as this is deprecated. I did try this approach also though but it returned a null value. In the admin for the particular product, the tax class is set to A_GEN_STANDARD for 20%
    – SNAFU
    Commented Jan 20, 2022 at 10:13
0

You can get tax rates by using \Magento\Tax\Api\TaxCalculationInterface:

    $rate = $this->taxCalculation->getCalculatedRate($productTaxClassId, $customerId, $storeId);
1
  • Hi @quickshiftin. Nope that didn't work either. The $customerId and $storeId values are null by default for that method. Regardless I tried plugging those values but still get 0 as the result, which is incorrect.
    – SNAFU
    Commented Jan 20, 2022 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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