Magento 2 customize price displayment
I show you how to customize price displayment for Magento 2. This is easier than in Magento 1, but you need to know a few things before you are changing only part of price displayment. Is there a Magento 2 price.phtml file?
Magento 2 customize price displayment
Magento 1 frontend price displayment is very complicated. There is only one price.phtml file that contains hundreds of lines of code and a bunch of if statements. To change a specific price, you had to place debug echos to find the correct place.
Magento 2 splits this up. There are various price, final_price, configured_price or default *.phtml files that lets you change price output for specific modules.
Where to find frontend price phtml files?
Magento 2 price displayment is completely different to Magento 1. Each module handles its own price phtml files. Turn on frontend path (Stores->Advanced->Developer->Debug->Enabled Template Path Hints for Storefront) and take a look. Different views:
- product detail page
- product list
- cart
- …
Are using different phtml files from different modules. There is also a major difference between simple, configurable and bundle products. If you want to add something to your product price displayment, you need to consider to change at least 3 phtml files for product detail page. Beside that, there is a difference between
- amount/default
- configured_price
- final_price
and each of this uses different if statements to allow displayment for optional special prices or manufacturer’s suggested retail price.
Easy?
The sounds quite complicated. Try to set some debug echos and learn how each file is used for display prices on frontend. You will get a clue how things work in Magento 2. With a bit exercise you will be able to modify used block (like \Magento\Catalog\Pricing\Render\FinalPriceBox or \Magento\Catalog\Pricing\Price\RegularPrice) and to manipulate prices in your own Magento 2 module.
Conclusion
Customize price displayment is easy with Magento 2. Each specific core module has different price *.phtml files with only some lines of code. You are able to find correct frontend files easily. Changing price display was never that easy.
1 Response
[…] Magento 2 customize price displayment by Magento2 Blog […]