If your OpenCart version is lower than 3.0.3.9, then your system is not compatible with PHP 8. Unfortunately, OpenCart 3 does not have an automatic update to upgrade to the latest version 3.0.4.0. However, the compatibility problem can be solved by adapting the old version of OpenCart to the requirements of PHP 8.x.
Our instructions are intended for OpenCart 3.0.2.0, 3.0.3.1, 3.0.3.2, 3.0.3.3, 3.0.3.5, 3.0.3.6, 3.0.3.7, 3.0.3.8. Do not forget to make a full backup of your site before performing any operations.
There are several methods to update an old version of OpenCart 3.0 to support PHP 8:
- Clone OpenCart libraries and individual files from the latest build to your system
- Update the libraries through composer and make some edits.
- Create a new site on OpenCart 3.0.4.0 and transfer the template, database and all modules to the new system.
Method 1: Clone files from the latest OpenCart 3 build
This method is the easiest and fastest, here's what you need to do:
- You need to download the latest version of OpenCart 3, currently it is 3.0.4.0
- Unzip the downloaded archive on your PC and open the upload folder
- Copy the system folder to your website, replacing the corresponding folders and files
- Copy the system/storage folder to your site in your 'storage' directory (it may be located outside the domain directory, as OpenCart requires you to move the storage outside the domain directory after installation). If you do not know where your storage is located, open config.php and find DIR_STORAGE.
- Copy and replace the file catalog/controller/event/theme.php
- Fix minor PHP errors (see below for a list of possible errors)
Method 2: Updating libraries via composer in terminal
- Determine the location of the storage directory on your site - open config.php and find DIR_STORAGE, the path to the directory is indicated there.
- Upload the composer.json file to this directory (download)
- Open a terminal (console) on the server and run the following commands sequentially:
cd path/to/directory/storage
composer update - If you have OpenCart below 3.0.3.7, you need to replace some system files:
catalog/controller/event/theme.php (download)
system/library/template/twig.php (download) - Fix minor PHP errors (see below for a list of possible errors and fixes)
Method 3: New website with data migration
This is probably the most complicated method, as it requires creating a new website with a consistent transfer of all data. We do not recommend this method as it is equivalent to creating a new website. In this case, you can develop a site on a new build, currently OpenCart 4.1.0.3, and get full compatibility with PHP 8, as well as the use of newer libraries and frameworks.
If you have decided to migrate to a new OpenCart 3 website with PHP 8 compatibility, here is a quick guide:
- Download and install the latest version of OpenCart 3, currently 3.0.4.0.
- Replace the database with yours - delete all tables and import your database dump.
- Copy the image folder from your old site to the new one
- Install the template and all necessary modules for OpenCart 3.
List of possible PHP errors and their fixes
After the transition of OpenCart 3.0 to new versions of PHP, especially PHP 8.0, 8.1, 8.2, 8.3, 8.4, errors, warnings and deprecations often appear. This is due to the fact that OpenCart 3 was created for PHP 7.0-7.3. Below are the most common errors, their explanations and recommendations:
Unknown: mysqli::real_escape_string(): Passing null to parameter #1 ($string) of type string is deprecated in /system/library/db/mysqli.php on line 46
Cause: in PHP 8.1+ you cannot pass null instead of the required string parameter.
Solution: add a check or cast to the type of the problem variable:
return $this->connection->real_escape_string($value);
replace with:
return $this->connection->real_escape_string((string)$value);
Unknown: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in /catalog/model/catalog/product.php on line 45
Cause: In PHP 8, calling round(null) is no longer allowed - null is not a valid argument type.
Solution: Before calling round(), you need to make sure that the argument is not null or do a type conversion:
'rating' => round($query->row['rating']),
replace with:
'rating' => round((float)$query->row['rating']),
Unknown: Automatic conversion of false to array is deprecated in /system/library/cart/user.php on line 43
Cause: The value that was false is automatically converted to array, which is now deprecated and potentially dangerous
Solution: Add an array check before the problematic line: if (is_array($var))
Unknown: Creation of dynamic property Request::$request is deprecated in /system/library/request.php on line 26
Unknown: Creation of dynamic property Session\DB::$db is deprecated in/system/library/session/db.php on line 8
Unknown: Creation of dynamic property Proxy::$getTranslations is deprecated in /system/engine/proxy.php on line 30
Unknown: Creation of dynamic property Cart\Cart::$config is deprecated in /system/library/cart/cart.php on line 7
Cause: in PHP 8.2+ you cannot create object properties on the fly.
Solution: Declare the property in the class in advance, at the beginning of the php class, for example:
private $request;
private $db;
private $proxy;
private $cart;