PrestaShop 9 errors
Call to undefined method Tools::jsonEncode() after upgrading PrestaShop
Tools::jsonEncode() and Tools::jsonDecode() were removed in PrestaShop 8.0. Stores that jump from 1.7 straight to 9 meet this error in older modules. The fix is one line.
- The error
Call to undefined method Tools::jsonEncode()- Removed in
- PrestaShop 8.0
- Usually found in
- Older modules, overrides
What happened
Tools::jsonEncode() and Tools::jsonDecode() were thin wrappers around PHP's own json_encode() and json_decode(), left from the time PHP did not always have JSON support. PrestaShop 8.0 removed them.
Stores that upgrade from 1.7 straight to 9 skip version 8, so they meet the removals of both versions at once. This one is among the most common, because almost every module that talks to JavaScript or saves settings as JSON used it.
The fix
Call PHP's functions directly. They take the same arguments:
// Before
$json = Tools::jsonEncode($data);
$data = Tools::jsonDecode($json, true);
// After (works in every PrestaShop version)
$json = json_encode($data);
$data = json_decode($json, true);
If the old code relied on Tools::jsonDecode() returning an object, call json_decode($json) without the second argument; it returns an object too.
Where else it hides
grep -rn "Tools::json" modules/ themes/ override/
The same modules often use other helpers removed in 8.0, such as Media::getJqueryPath() (see jQuery files return 404) or Cookie::isLogged() (use $this->context->customer->isLogged() instead). If the store is still on 1.7, expect PHP 8 errors too: PrestaShop 9 needs PHP 8.1 or newer, see Call to undefined function each().