My company is using an extension to create a custom product which is unique in that the pricing goes out 4 decimal places. There was a whole lot that went into this prior to me getting involved, but all this is in place already and working.
The problem I'm dealing with today is that the price is automatically rounding on the front end, and I want to change it so that it shows the pricing with all 4 decimal places so that people know the actual price of the item when viewing the product.
Here is my plan to do this:
- Extend the class
Magento\Framework\Pricing\Render\Amount
and add a function that will output the price with 4 decimal places, without effecting any other functionality. - Create a new template that uses the new class, and keep it the same except the price display, which will use the new function to display the full price.
- Use xml file to replace the default price display with this template
Is this the correct way to do this? I don't expect a full tutorial, of course, so I just want to know if I'm at least at a good starting point… although any other helpful advice would be appreciated.
Running Magento 2.1.2
Best Answer
From what I found the pricing precision is defined by
Magento\Framework\Pricing\PriceCurrencyInterface::DEFAULT_PRECISION
and I didn't see any implementing classes that changes this. I was able to accomplish this with of plugins. This should point you down the right path on the backend. The increased precision is visible on the frontend, but on product view pagesprice-utils.js
formats the price after the page loads and changes the precision back to two even after theMagento\Framework\Locale\Format
plugin updates the frontend precision configurations.I believe this is caused by this line in
Magento/Catalog/view/base/web/js/price-utils.js
around line 73I wound up overriding this file in the module and changing the line to
I'm sure there is a better way to do that, but I had to move on. The code for the plugins is below:
Vendor/Module/etc/di.xml
Vendor/Module/Plugin/Framework/Pricing/Render/Amount.php
Vendor/Module/Plugin/Directory/Model/PriceCurrency.php
Vendor/Module/Plugin/Framework/Locale/Format.php