5

I have simple product name is "Test Product" with Price = 100$ Also, I assign the tax class name "Default" with 7% Tax Rate

So how can I Calculate the product price including tax?

Default.phtml

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$ProductId = 1;
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($ProductId);    
$taxCalculation = $objectManager->create('\Magento\Tax\Api\TaxCalculationInterface');
$scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface');    

if ($taxAttribute = $product->getCustomAttribute('tax_class_id')) {
    $productRateId = $taxAttribute->getValue();        
    $taxCalculation = $objectManager->create('Magento\Tax\Model\Calculation\Rate')->load($productRateId);
    $rate = $taxCalculation->getRate();

    if ((int) $scopeConfig->getValue('tax/calculation/price_includes_tax', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) === 1) {
        $priceExcludingTax = $product->getPrice() / (1 + ($rate / 100));
    } else {
        $priceExcludingTax = $product->getPrice();
    }        
}
$priceIncludingTax = $priceExcludingTax + ($priceExcludingTax * ($rate / 100));

Thanks in Advance!

3 Answers 3

11

Simply use Magento\Catalog\Helper\Data::getTaxPrice($product, $product->getFinalPrice(), true)

EDIT with code example:

In your constructor, inject like this:

public function __construct(\Magento\Catalog\Helper\Data $taxHelper) {
    $this->taxHelper = $taxHelper;
}

...and use it in your code like this:

public function doSomething($product) {
    $price = $this->taxHelper->getTaxPrice($product, $product->getFinalPrice(), true);
    // do something with the price
}
5
  • please update full code. Nov 16, 2018 at 12:38
  • This gave me price with excluding tax. How to get including VAT? Nov 19, 2018 at 6:46
  • Then your tax settings are wrong, or your code is called incorrectly, for example it is not executed inside the correct store view. The method is defined in vendor/magento/module-catalog/Helper/Data.php and the 3rd parameter is * @param bool $includingTax return price include tax flag, which is set to true in my example; using Magento 2.1.14 Nov 19, 2018 at 8:22
  • 2
    Magento 2.3.x \Magento\Catalog\Helper\Data::getTaxPrice() can work. In this function there is if ($this->_taxConfig->needPriceConversion($store)) {. If conversion isn't need by store configuration than the same price is returned. I guess we can use own helper class with same function without needPriceConversion. The function should be called only for display and only once to display price in a particular zone. Avoid adding twice tax. Apr 22, 2019 at 14:41
  • @obscure true, need to some how ignore that to make it work. Also, i used this code instead of @simonthesorcerer code: $price = $this->taxHelper->getTaxPrice($product, $product->getFinalPrice(), true, null, null, null, null, true, true);
    – fudu
    Dec 27, 2019 at 8:09
0

Try this one

$_product->getPriceInfo()->getPrice('final_price')->getValue()
-2

I tried this which is works fine for me.

Check out this link.

2
  • I also refer this link but I didn't get the including tax value see my code
    – Rajan Soni
    Jan 8, 2018 at 12:01
  • This is surprisingly the only way I could calculate the correct prices using the checkout session quote object in Magento 2.4.5. Dec 7, 2022 at 8:18

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.