PrestaShop 9 errors
Call to undefined method Tools::displayPrice() in PrestaShop 9
PrestaShop 9 removed Tools::displayPrice(). Any module or override that formats a price with it stops with a fatal error. Here is the replacement and where to look.
- The error
Call to undefined method Tools::displayPrice()- Removed in
- PrestaShop 9.0
- Usually found in
- Modules, overrides
What happened
Tools::displayPrice() turned a number into a price with the currency sign, for example 12.5 into €12.50. PrestaShop moved price formatting to its localization component years ago and kept the old method as a shortcut. In PrestaShop 9.0 the shortcut is gone.
Code that still calls it now ends with a fatal error. If the call runs on every page (a header block, a product list, a cart summary), the whole store can show a blank page or error 500. If it runs only in one place, only that place breaks, for example a module's settings page or one email.
The fix
Format the price through the current locale instead:
// Before (PrestaShop 1.7 and 8)
$formatted = Tools::displayPrice($price, $currency);
// After (works in 1.7.6+, 8 and 9)
$context = Context::getContext();
$formatted = $context->getCurrentLocale()->formatPrice($price, $context->currency->iso_code);
If the old call passed a currency, use that currency's ISO code:
- a
Currencyobject:$currency->iso_code - a currency ID:
Currency::getIsoCodeById((int) $idCurrency)
formatPrice() follows the store's language settings, so the result matches the prices PrestaShop shows elsewhere.
In templates
Front office templates should not format prices themselves. PrestaShop's presenters already pass formatted prices to the theme (for example {$product.price}), so a template that needs a price should receive it ready from the PHP side. The {displayPrice} Smarty function exists only in the back office, in every version, and so does the 1.6-era {convertPrice} tag: in a front office template both stop with unknown tag. For an amount the module calculates itself, such as a voucher or a fee, format it in the hook with formatPrice() and assign the ready string to the template.
Where else it hides
Tools::displayPrice() is rarely alone. A module written in the same period often also uses Tools::displayNumber(), Tools::jsonEncode() or ajaxDie(), all removed in PrestaShop 8 or 9. Search the whole module folder, the theme and the override/ folder, not only the file named in the error:
grep -rn "displayPrice\|displayNumber" modules/ themes/ override/
Keep in mind that third-party modules lose your edits at their next update. If the author has a PrestaShop 9 version, install that instead.