3

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!

8

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
}
| improve this answer | |
  • please update full code. – jafar pinjar Nov 16 '18 at 12:38
  • This gave me price with excluding tax. How to get including VAT? – jafar pinjar Nov 19 '18 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 – simonthesorcerer Nov 19 '18 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. – obscure Apr 22 '19 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 '19 at 8:09
0

I tried this which is works fine for me.

Check out this link.

| improve this answer | |
  • I also refer this link but I didn't get the including tax value see my code – Rajan Soni Jan 8 '18 at 12:01

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.