How to remove the price decimal from Front end price display only on the category and product pages ?
-
For the front end price display on the category and product pages? or are you asking about another part of the site? – circlesix Jun 25 '16 at 18:24
7 Answers
If you turn on the template hints for your site you will see the price is set in this template:
/vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml
Copy this file into your theme
(app/design/frontend/{{namespace}}/{{theme_name}}/Magento_Catalog/templates/product/price/amount/default.phtml).
Take note of the class that this file is defined from
\Magento\Framework\Pricing\Render\Amount
The price itself is coming from this call:
<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
If we take a look at the file that this method is created based on the class it's inheriting (/vendor/magento/framework/Pricing/Render/Amount.php) you will see this method:
/**
* Format price value
*
* @param float $amount
* @param bool $includeContainer
* @param int $precision
* @return float
*/
public function formatCurrency(
$amount,
$includeContainer = true,
$precision = PriceCurrencyInterface::DEFAULT_PRECISION
) {
return $this->priceCurrency->format($amount, $includeContainer, $precision);
}
Taking note of the perams in the method, we can see from the template that is displaying the price that we have the definition for the $amount and the $includeContainer but not for the last one $precision which defaults to the constant PriceCurrencyInterface::DEFAULT_PRECISION. This constant is defined in the file /vendor/magento/framework/Pricing/PriceCurrencyInterface.php. Looking in this file we see at the top of the file just after the interface is defined const DEFAULT_PRECISION = 2;. So in the templale we just have to redefine this constant and it will change the default precision based on this value. Setting the value to 0 will do the trick for the category page and for the product page.
Change:
<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
to:
<?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer(), 0) ?>
Note
This works on the site that I tested on, using USDs. Because Magento supports international currencies and different translations, this method might not work exactly as it does for me. But I don't have any international site set up right now, so I'm not able to test.
- 3,139
- 2
- 15
- 32
- 4,273
- 3
- 27
- 57
-
That is perfect working fine wherever there is a price except in product page – Ahmad Darwish Jun 26 '16 at 19:50
-
Yeah, i just saw that. The price get reloaded and changed with some JS in this file
/vendor/magento/module-catalog/view/base/web/js/price-box.js. I just need to look into how to override this behavior. – circlesix Jun 26 '16 at 22:42 -
I found this while looking around, it's not an answer to the question, but might get you down the right path. http://magento.stackexchange.com/questions/112102/magento2-how-can-i-override-core-js-module-price-box-js – circlesix Jun 27 '16 at 00:08
-
I think this in combination with the JS solution is the right answer to the question. PHP handles rendering the prices from the server, whereas JS handles rendering the dynamically written prices (like in the minicart or when switching between options for configurable products). – Giel Berkers Mar 02 '17 at 08:32
-
1this is awesome answer.. can you tell me how to remove decimal from product details page – Sarvesh Tiwari May 03 '18 at 11:22
To correct the product page you also should modify next javascript:
/vendor/magento/module-catalog/view/base/web/js/price-utils.js
on line 38 change
var precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision,
for
var precision = 0,
Hope this help.
I found this in: Magento 2 : How to remove decimal points from the price
-
Don't modify core files, since they will be overwritten when you upgrade or re-install your installation. Rather use an override in your own design like where the article links to suggests. – Giel Berkers Mar 02 '17 at 08:31
-
Therefore you would have to add a requirejs-config.js file to some module (/app/code/VENDOR/MODULE/view/frontend/requirejs-config.js) with content var config = {
map: { '*': { "Magento_Catalog/js/price-utils": "VENDOR_MODULE/js/price-utils" } }}; Then create a file price-utils.js like /app/code/VENDOR/MODULE/view/frontend/web/js/price-utils.js. For German explanation please see here
– Bastian Kretzschmar Mar 31 '21 at 09:21
The price-utils.js solution did not work for me.
But this worked in Magento 2.1: Look in
/vendor/magento/framework/Pricing/PriceCurrencyInterface.php
Change
const DEFAULT_PRECISION = 2;
To
const DEFAULT_PRECISION = 0;
Then flush cache.
- 5,209
- 13
- 62
- 103
- 167
- 1
- 1
-
Hi, I tried this answer and changed price in frontend only. how to change in backend also ? – Jsparo30 May 01 '17 at 12:41
-
1
-
you can find proper override of PriceCurrencyInterface using custom module in that answer https://magento.stackexchange.com/a/211894/23264 – electroid May 11 '22 at 20:33
You can use this module
if you are using composer, just run
composer require lillik/magento2-price-decimal
- 21
- 1
-
This module caused several issues for us, product prices was corrupted – cjohansson Mar 01 '18 at 08:17
-
While you create new product it mess up all product prices, if a product price is 2,500 it will become 2 as it will remove all digits which come after comma (,) in price. – Naveed Asim May 26 '18 at 17:41
-
Its not working , product price getting corrupted totally.......... – Chala Chalapathi Sep 23 '19 at 11:20
Change this in the file price-utils.js
from
(precision ? decimalSymbol + am.toFixed(2).replace(/-/, 0).slice(2) : '');
to
(precision ? decimalSymbol + am.toFixed(format.requiredPrecision).replace(/-/, 0).slice(2) : '');
It will dynamically load price precision and you don't have to do anything in js file.Just change in php file and will reflect here :)
- 1
- 3
I fixed method formatPrecision in vendor/magento/module-directory/Model/Currency.php on line ~301 there was code:
if (!isset($options['precision'])) {
$options['precision'] = $precision;
}
I fixed so
if (!isset($options['precision'])) {
$options['precision'] = ( (floatval($price) - intval($price)) != 0 ? $precision : 0);
}
- 1
- 2
To get desired result you have to edit this file
/pub/static/frontend/Theme-Package/Theme-Name/en_US/Magento_Catalog/js/price-utils.js
at line 38
var precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision,
replace this with
var precision = 0,
This is for magento2.1.3 this will remove decimals from price digits. This solution helped me
- 3,816
- 5
- 32
- 69
- 87
- 1
- 8
-
Realize that static generated files (the files in
pub/static) are overwritten every time you runsetup:static-content:deploy. So I wouldn't make this modification here, but rather override/vendor/magento/module-catalog/view/base/web/js/price-utils.js, like Diego suggested. – Giel Berkers Mar 02 '17 at 08:30 -