1

I want to make a modification with the price shown in catalog view page so i have to modify _toHtml() in module-catalog\pricing\render.php I am not sure what is the best way to do that.

for example, for listing page, i used a plugin for the aftergetProductPrice() function of catalog\Product\ListProduct.php

1

If you just want to rewrite the price of listing page, it is fine in this case. But in most cases, we want to rewrite all the price in all page.

In those cases, you should rewrite method format of \Magento\Directory\Model\PriceCurrency class.

P/s: you can either using prefences or plugin

Edited: If you want to also modify the price in product view page, checkout page, cart page (these pages using knockoutjs to render the price format).

Rewrite the formatPrice() method in Magento_Catalog/web/js/price-utils

For me, i added the configuration in admin dashboard. And then i add these line of code to the formatPrice() method:

 if (typeof window.precision !== 'undefined') {
            precision = window.precision;
        }

This is the eddited method:

 /**
 * Formatter for price amount
 * @param  {Number}  amount
 * @param  {Object}  format
 * @param  {Boolean} isShowSign
 * @return {String}  Formatted value
 */
function formatPrice(amount, format, isShowSign) {
    format = _.extend(globalPriceFormat, format);

    // copied from price-option.js | Could be refactored with varien/js.js

    var precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision,
        integerRequired = isNaN(format.integerRequired = Math.abs(format.integerRequired)) ? 1 : format.integerRequired,
        decimalSymbol = format.decimalSymbol === undefined ? ',' : format.decimalSymbol,
        groupSymbol = format.groupSymbol === undefined ? '.' : format.groupSymbol,
        groupLength = format.groupLength === undefined ? 3 : format.groupLength,
        pattern = format.pattern || '%s',
        s = '',
        i, pad,
        j, re, r, am;

    if (typeof window.precision !== 'undefined') {
        precision = window.precision;
    }


    if (isShowSign === undefined || isShowSign === true) {
        s = amount < 0 ? '-' : (isShowSign ? '+' : '');
    } else if (isShowSign === false) {
        s = '';
    }
    pattern = pattern.indexOf('{sign}') < 0 ? s + pattern : pattern.replace('{sign}', s);

    // we're avoiding the usage of to fixed, and using round instead with the e representation to address
    // numbers like 1.005 = 1.01. Using ToFixed to only provide trailig zeroes in case we have a whole number
    i = parseInt(amount = Number(Math.round(Math.abs(+amount || 0) + 'e+' + precision) + ('e-' + precision)) , 10) + '';
    pad = (i.length < integerRequired) ? (integerRequired - i.length) : 0;

    i = stringPad('0', pad) + i;

    j = i.length > groupLength ? i.length % groupLength : 0;
    re = new RegExp('(\\d{' + groupLength + '})(?=\\d)', 'g');

    // replace(/-/, 0) is only for fixing Safari bug which appears
    // when Math.abs(0).toFixed() executed on '0' number.
    // Result is '0.-0' :(


    am = Number(Math.round(Math.abs(amount - i) + 'e+' + precision) + ('e-' + precision));
    r = (j ? i.substr(0, j) + groupSymbol : '') +
        i.substr(j).replace(re, '$1' + groupSymbol) +
        (precision ? decimalSymbol + am.toFixed(2).replace(/-/, 0).slice(2) : '');

    return pattern.replace('%s', r).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

Note: the window.precision is the global variable that i assigned to when loading the page.

| improve this answer | |
  • The suggestion is not working on product view page. – arushi Jul 23 '17 at 5:24
  • Yeah. some places magento use knockoutjs to render the price format (product detail page, checkout page). Do you want to fix that, too ? – Toan Tam Jul 23 '17 at 5:55
  • Basically, i want to have a solution for product view page, as i am not getting the way from where o modify it. I am successfully able to do it on list and front page blocks. If u can just guide me with product view page that will be helpful. As currently i can see that it is coming from module-catalog\pricing\render.php but i am not sure how to override this and what will be the best way to modify that price. – arushi Jul 23 '17 at 5:59
  • I have just updated my answer, please have a look at that – Toan Tam Jul 23 '17 at 6:10
  • I tried to return some random value from format price function but it did not make any change. – arushi Jul 23 '17 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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