Onlineshop Magento 2: Preise ohne Nachkommastellen Titelbild

Current issue at our web agency while developing an online shop based on Magento 2: How can prices be displayed without decimal places in both the product detail view and the product list view? The good news: Magento 2 allows an optional $precision parameter when formatting the price with the formatCurrency() function, which is responsible for the display / rounding. The bad news: In the detail view, the price is formatted again using JavaScript.

 

 

This means: we have to make changes in 2 places. First, we overwrite the template file responsible for displaying the price. To do this, we create the file

 

/app/design/frontend/PACKAGE/TEHEME/Magento_Catalog/templates/product/price/amount/default.phtml

 

Here we add the parameter mentioned above, in our case with "0", so that the line

 

  <?= $block->escapeHtml($block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()), ['span']) ?>

 

changes to

 

  <?= $block->escapeHtml($block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer(),0), ['span']) ?>

 

This removes the decimal places from the prices in the list view. Now we still need to take care of the product detail view. Here, the price is loaded without decimal places, but is subsequently modified by JavaScript, more precisely by price-utils.js from the Mage_Catalog module.

 

We now overwrite this by creating a requirejs-config.js in any module:

 

/app/code/VENDOR/MODULE/view/frontend/requirejs-config.js

 

with the following content:

 

var config = {

    map: {

        '*': {

            "Magento_Catalog/js/price-utils": "VENDOR_MODULE/js/price-utils"

        }

    }

};

 

and then, according to the defined mapping, create the file

 

/app/code/VENDOR/MODULE/view/frontend/web/js/price-utils.js

 

which corresponds to the core file.

 

Here, we only need to insert the following statement before the line

 

precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision;

 

the following instruction:

 

format.requiredPrecision = 0;

 

When making changes to price-utils.js, always make sure to delete the corresponding pub/static file

 

/pub/static/frontend/PACKAGE/THEME/de_DE/VENDOR_MODULE/js/price-utils.js

 

That's it. Seen at our web agency in an online shop running Magento 2.3.5.