Showing Both Inclusive and Exclusive VAT Prices for Single Products in Magento
As an e-commerce developer working with Magento, you may often find yourself needing to display both inclusive (VAT included) and exclusive (VAT excluded) prices for a single product on your storefront. While Magento provides this functionality by default, it’s not straightforward when you want to apply it selectively to certain products.
Let me walk you through the steps required to achieve this without resorting to hacking core files like price.phtml.
The Default Behavior
By default, Magento shows prices including VAT for taxable goods and excluding VAT for non-taxable ones. The decision is based on whether a product has tax classes assigned.
In your case, you want to override this behavior for specific products, displaying both prices if an attribute (e.g., show_both_prices) is set to Yes.
The Core Hack
You mentioned modifying the price.phtml file in your theme’s template directory:
<?php if ($_taxHelper->displayBothPrices() || $_product->getData('show_both_prices')): ?>
While this works, it modifies a core file and isn’t an optimal solution. Let’s explore how to do this cleanly.
The Recommended Approach
We’ll create a custom tax helper that overrides the default behavior based on our desired criteria.
- Create a new module: Let’s call it
Company_TaxPriceHelper. In theapp/code/local/Company/TaxPriceHelperdirectory, create anetc/config.xmlfile:
<?xml version="1.0"?>
<config>
<modules>
<Company_TaxPriceHelper>
<active>true</active>
<codePool>local</codePool>
</Company_TaxPriceHelper>
</modules>
</config>
- Override the tax helper: Create a new file
app/code/local/Company/TaxPriceHelper/etc/config.xml:
<?xml version="1.0"?>
<config>
<global>
<models>
<tax>
<rewrite>
<helper>Company_TaxPriceHelper_Helper_Data</helper>
</rewrite>
</tax>
</models>
</global>
</config>
- Create the custom helper: In
app/code/local/Company/TaxPriceHelper/Helper/Data.php:
class Company_TaxPriceHelper_Helper_Data extends Mage_Tax_Model_Helper_Data
{
public function displayBothPrices()
{
$show_both_prices = Mage::registry('current_product')->getData('show_both_prices');
if ($show_both_prices == 1 || $_taxHelper->displayBothPrices()) {
return true;
}
return false;
}
}
-
Update your product attributes: Set the
show_both_pricesattribute to ‘Yes’ for products where you want both prices displayed. -
Display the prices in templates: In your theme template files (e.g.,
price.phtml), use the following code:
<?php if ($_taxHelper->displayBothPrices()): ?>
<p class="price-info">
Excl. Tax: <?php echo Mage::helper('core')->currency($_product->getPrice(), true, false); ?>
</p>
<p class="price-info">
Incl. Tax: <?php echo Mage::helper('core')->currency($_product->getPrice(), true, true); ?>
</p>
<?php endif; ?>
Conclusion
By following these steps, you’ve successfully overridden the default Magento behavior to display both inclusive and exclusive VAT prices for specific products without modifying core files.
This approach ensures that your code remains portable and maintainable. It also provides a clear separation between your custom functionality and Magento’s core logic, making future updates and upgrades easier.
Remember, when working with tax calculations in Magento, it’s crucial to follow the correct order of operations outlined in the official documentation.
With this implementation, you can now efficiently manage product pricing based on your specific business requirements.