Magento – Cannot get Magento 2 catalog price incl tax to work

magento2pricetaxvat

I have a problem with Magento 2, I have configured a tax class and settings for including it in catalog and entered price in admin. Still when I get priceinfo I get this price:

For a product of price 119:

$prices = $product->getPriceInfo()->getPrices();
var_dump($prices->current()->getAmount());

Outputs:

object(Magento\Framework\Pricing\Amount\Base)[2082]
   protected 'amount' => float 148.75
   protected 'baseAmount' => null
   protected 'totalAdjustmentAmount' => null
   protected 'adjustmentAmounts' => 
     array (size=1)
       'tax' => float 29.750001
   protected 'adjustments' => 
     array (size=0)
       empty

Why is magento adding the tax on top of 119 despite my settings?

I have done some further research in this, it seems magento2 has a problem counting taxes backwards?

When running \Magento\Catalog\Helper\Data function getTaxPrice I set price at 119 to include tax and to return price without tax:

https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Helper/Data.php#L515

Should not this function return 119 * 0.8 = 95.2 in this case? As my tax is 25% and backwards tax that is 20%.

I run the function as this:

$catalogHelper->getTaxPrice($product, $product->getPrice(), false, null,null,null,null,true,true)

It returns 119 to me which seems weird.

Best Answer

My solution for base prices incl. vat is

$product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();

For special price it will be

$product->getPriceInfo()->getPrice('special_price')->getAmount()->getValue();
Related Topic