I'm trying to get the tax rate (just the value) for a product. I can get the Tax Class Id doing $product->getTaxClassId().

It seems incredible, but I can't find the way to get the value. Not the Price with the included tax or the tax amount but the percentage applied to the final price.

Can anyone help?

Thanks in advanced.

有帮助吗?

解决方案

To get the tax rate you need to perform a tax request. That can be done with the following code.

$store = Mage::app()->getStore('default');
$taxCalculation = Mage::getModel('tax/calculation');
$request = $taxCalculation->getRateRequest(null, null, null, $store);
$taxClassId = $product->getTaxClassId();
$percent = $taxCalculation->getRate($request->setProductClassId($taxClassId));

Here you take the store and make a tax calculation request. You can also specify shipping address, billing address and customer tax class id but in this case I have set them to null and it will take the default values set-up in the admin configuration tax section.

You then take the tax rate request object and assign the product class id, which you already have and then you can ask for the rate itself.

其他提示

          
public function __construct(        
        \Magento\Tax\Model\Calculation $taxCalculation,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->taxCalculation = $taxCalculation;
        $this->_storeManager = $storeManager;        


    } 
function getTaxPercentage(){
       // Tax Calculation
          $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);
          $taxPercent = $this->taxCalculation->getRate($request->setProductClassId($productTaxClassId))
    }
许可以下: CC-BY-SA归因
scroll top