Displaying Tax Class on Magento Product Page
In Magento, you can display the tax class assigned to a product directly on the product page. This allows customers to see exactly which tax rate is applied during checkout. Here’s how you can achieve this with a simple code override.
Accessing the Tax Class
To access the tax class information for a specific product, we need to retrieve it from Magento’s taxonomy and apply it to our template file.
Firstly, let me explain some context. In Magento, taxes are configured under System > Tax. Each tax rate is assigned to one or more tax classes (e.g., “Taxable Goods,” “Taxable Services”). These tax classes are then applied directly to products in the product edit page, under the “Tax Class” dropdown.
Overriding the Product View Template
To display this information on the front-end, we will need to override the catalog/product/view.phtml template file. This is a core Magento template located at:
vendor/magento/module-catalog/view/frontend/templates/product/view.phtml
We’ll create an equivalent file in our custom theme:
app/design/frontend/Your_Vendor/your_theme/Magento_Catalog/templates/product/view.phtml
Adding the Tax Class to the Price
Now, let’s add the tax class information to the price on the product view page. Open or create your view.phtml file and find the following line:
<?= /* @escapeNotVerified */ $block->getProductPriceHtml($_product) ?>
Just before this line, you can add the following code snippet:
<?php
$taxClassId = $_product->getTaxClassId();
$taxClassName = Magento\Tax\Model\ClassModel::load($taxClassId)->getTitle();
?>
<p>
Price: <?= /* @escapeNotVerified */ $block->getProductPriceHtml($_product) ?>
+ <?= /* @escapeNotVerified */ $block->getProductPriceHtml($_product, $_product->getFinalPrice(), 'minimal') ?> - <?= /* @escapeNotVerified */ $taxClassName ?>
</p>
This code retrieves the tax class ID from the product object, loads its corresponding title using the Magento\Tax\Model\ClassModel model, and then displays it alongside the price. The getProductPriceHtml() function is used to format the final price according to Magento’s configuration.
Conclusion
With this simple override, you can now display the tax class name directly on the product page in your Magento store. This provides additional transparency to customers about the pricing structure and tax rates applied to their purchases.
Remember to always use a child theme for overriding core files, as changes made to core files may be overwritten during updates. Also, consider using a proper Magento 2 development environment to test these changes before deploying them to your live site.
For more information on customizing Magento templates and extending the core functionality, I recommend checking out our comprehensive guide at https://laravelcompany.com/magento-development/.