Magento 2: Retrieving Product Price With Tax Included
Introduction
In Magento 2, retrieving a product’s price including tax can be achieved through the built-in tax calculation functionality. Unlike Magento 1 where you needed to manually apply taxes using the taxHelper, Magento 2 handles this automatically for you. In this guide, we’ll explore how to retrieve prices with tax included within a .phtml file in Magento 2.
Understanding Magento 2 Tax Calculation
Magento 2 performs tax calculation based on the product’s price and the applicable tax rules configured in the backend. When a customer views a product page, Magento automatically applies the correct tax rates based on their location and the store’s configuration. This ensures that customers always see prices inclusive of taxes for their specific area.
Retrieving Price with Tax Included
To retrieve a product’s price including tax within a .phtml file in Magento 2, you can simply access the price attribute directly. Magento will automatically apply the correct tax rate based on the customer’s location and store configuration when fetching this value.
Example: Displaying Product Price Inclusive of Tax
<?php $product = $block->getProduct(); ?>
<div class="price-box">
<span class="price">Price incl. VAT:
<?= $product->getPrice() ?></span>
</div>
In the above example, <?= $product->getPrice() ?> retrieves and displays the product’s price inclusive of tax for the current customer.
Retrieving Tax Amount Separately
While Magento handles tax calculation automatically when fetching prices, there might be scenarios where you want to access the tax amount separately. For this purpose, you can use the taxCalculation model.
Example: Calculating and Displaying Tax Amount
$product = $block->getProduct();
$taxCalculator = $objectManager->create('Magento\Tax\Model\Sales\Total\Quote\Tax');
$taxAmount = $taxCalculator->calcTaxAmount($product, $product->getFinalPrice(), 1, true);
echo "Tax Amount: " . number_format($taxAmount, 2);
In this example, we first create an instance of the Magento\Tax\Model\Sales\Total\Quote\Tax class using Magento’s object manager (although you should avoid using it directly in production code). We then use the calcTaxAmount() method to calculate and retrieve the tax amount for the given product.
Conclusion
Retrieving a product’s price inclusive of tax in Magento 2 is simplified compared to Magento 1. By directly accessing the getPrice() method or using the taxCalculation model, you can easily fetch prices with taxes applied automatically based on the store configuration and customer location.
Remember that for any customizations involving the object manager, it’s recommended to use dependency injection in your own modules to ensure clean code and avoid direct access to Magento’s core objects. This approach also enhances the maintainability of your extensions.
For further insights into tax calculation in Magento 2 and best practices for extension development, be sure to explore our comprehensive guides on Magento 2 Tax Calculation and Dependency Injection in Magento 2.