5

I am trying to work with produce prices using 4 decimal places e.g. 0.0475

I have already got the prices to save and display in catalog admin by editing the following file...

vendor/magento/zendframework1/library/Zend/Currency.php (line 74) - I understand not to edit core files as a rule of thumb

Which file(s) do I edit to get prices on front end and cart to use these prices?

So my issue for example is if I order a quantity of 500 for 0.0475 the total should be 23.75, however it is displaying as 25.00

My price on the actual line of the basket does read 23.75, however the ajax cart thing on the right where it calculates Tax/Vat, reads 25.00

I have read I might need to edit these two files but I am not sure which bits...

vendor/magento/module-catalog/view/base/web/js/price-utils.js

vendor/magento/framework/Pricing/PriceCurrencyInterface.php

Thanks

1
  • Good point raised. +1 for that :) May 21, 2019 at 16:22

2 Answers 2

4

I raised this as an issue on githib. M team response was to either store prices inc vat or put up with it. I can understand their approach mind you. With all the localisation it's impossible to have a one size fits all solution. For example some currencies are rounded to nearest whole unit.

https://github.com/magento/magento2/issues/10532

In magento 1 we hacked core to accept 4 dp. But that caused issues with a lot of payment gateways.

3
  • Thanks for the feedback Dominic. I did see your post on github on my travels here. Including VAT in our prices doesn't really work for us either unfortunately e.g. 0.0475 *1.2 = 0.057 (so it is still more than 2 decimal places) May 22, 2019 at 11:19
  • If it helps we had issues with Paypal and Adyen. One more but I forget. The main issue is validation in the extensions telling you that you are sending incorrect amounts. May 22, 2019 at 11:29
  • Great thanks for the heads up May 22, 2019 at 13:26
2

You need to override three files and change price precision

Override the file in any of your module vendor/magento/framework/Pricing/PriceCurrencyInterface.php
And update

const DEFAULT_PRECISION = 4;

Override vendor/magento/module-directory/Model/Currency.php in any of your module
And update the line

return $this->formatPrecision($price, 4, $options, $includeContainer, $addBrackets);

Likewise override the file in any of your module vendor/magento/module-sales/Model/Order.php
And update the function

public function formatPrice($price, $addBrackets = false) {
    return $this->formatPricePrecision($price, 4, $addBrackets);
}

This should work.

Reference: Format price decimal points

2
  • Thanks Shoaib - that helped me in the right direction. It doesn't do it fully to what we need but a very good start May 22, 2019 at 11:17
  • That's great. Welcome :) May 22, 2019 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.