Magento 2: Displaying Prices Without Tax
As an experienced e-commerce developer, I often encounter this common challenge faced by merchants using Magento 2âdisplaying product prices both including and excluding tax in the frontend.
In your case, you’ve added code to display the base price without tax within the final_price.phtml template file. However, you’re experiencing a recurring issue where the excluded price is shown alongside the included price for configurable and simple products.
Understanding Magento 2 Price Display Logic
Magento 2 handles product pricing in a complex manner, especially when dealing with tax calculations. The platform uses various models to process prices, including Magento\Catalog\Model\Product, Magento\Framework\Pricing\Render, and Magento\Tax\Block\Form.
The core issue lies in how Magento 2 manages the display of prices based on the product type (simple, configurable, bundle, etc.) and the tax calculation strategy.
Modifying Price Display for Simple Products
For simple products, you want to ensure that only the price excluding tax is displayed. To achieve this, modify your code as follows:
<?= /* @escapeNotVerified */ $block->renderAmount($block->getProduct()->getFinalPrice(),
['price_id' => $block->getProductId() . '-final_price',
'include_container' => true,
'schema' => $schema]
); ?>
In this snippet, replace getFinalPrice() with getBasePrice(). The getBasePrice() method returns the price before tax is applied.
Ensure that you clear your cache after making these changes to see them reflected on the frontend.
Handling Configurable Products
Configurable products introduce additional complexity due to their variable nature. To display only the base price without tax for configurable products, update your code as follows:
<?= /* @escapeNotVerified */ $block->renderAmount($block->getProduct()->getBasePrice(),
['price_id' => $block->getProductId() . '-base_price',
'include_container' => true,
'schema' => $schema]
); ?>
Displaying Prices with and without Tax
If you want to display both the price including and excluding tax, consider utilizing Magento 2’s built-in functionality provided by the Magento\Framework\Pricing\PriceCurrencyInterface class.
First, inject this interface in your template file:
<block type="directory/currency" name="currency" as="currency" template="directory/currency.phtml"/>
Then, update your price display code as follows:
<?= /* @escapeNotVerified */ $block->renderAmount($block->getProduct()->getBasePrice(),
['price_id' => $block->getProductId() . '-base_price',
'include_container' => true,
'schema' => $schema]
); ?>
<br/>
<?= /* @escapeNotVerified */ $block->renderAmount(
$block->currencyCurrency->convertAndFormat($block->getProduct()->getPrice(), $block->currencyCurrency->getStoreCurrencyCode()),
['price_id' => $block->getProductId() . '-final_price',
'include_container' => true,
'schema' => $schema]
); ?>
This code displays the base price (excluding tax) followed by a line break, and then the final price including tax.
Conclusion
Displaying product prices both including and excluding tax in Magento 2 requires a deep understanding of the platform’s pricing mechanisms. By modifying the relevant template files and using Magento 2’s built-in pricing utilities, you can achieve the desired behavior for your storefront.
Remember to clear your cache after making changes to templates, as Magento 2 stores compiled frontend data in cached files.
For more advanced customizations or if you’re facing specific tax calculation challenges, I recommend exploring the Laravel Company blog for detailed tutorials and best practices on e-commerce development with Magento.