Compatibility of an OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → 3 and 4 module is easiest to achieve not with one archive for both branches, but with a shared business logic core and two thin packages: one separately for 3.0 and one for 4.1. The file structure, namespaces, routes, events, form saving, and part of the database schema differ between these versions so much that a single "for all versions" archive turns into a continuous if (version_compare(...)). Below we break down what exactly differs, how to split the code, and how not to break either branch in the next release.
Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.
Why doesn't one archive for both versions work?
The difference between 3.0 and 4.1 is not in individual methods, but in where the files are located and how the core finds them:
| What | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Module placement | files are spread across the core's admin/, catalog/, system/ | a separate extension/{код}/ directory with its own admin/, catalog/, system/ |
| Classes | global, name from the path: ControllerExtensionModuleMymod | namespaces: Opencart\Admin\Controller\Extension\Mymod\Module |
| Method route | slash: extension/ | dot: extension/ |
| Event triggers | .../ | .../ |
| Handler return value | before can cancel the call, after replaces the result | ignored, only arguments passed by reference change |
| Settings form | POST and redirect | AJAX saving with a JSON response |
| Package | ocmod.zip with a upload/ directory and, if needed, install.xml | ocmod.zip with install.json at the root and a package structure |
| Minimum PHP | 7.3 (3.0.4.1), 8.1 (3.0.5.1) | 8.1 |
The 3.0 core looks for a controllerCode that receives a request and prepares data. More in the glossary → by path in catalog/controller/, while 4.1 uses the namespace autoloader of the installed package. A file that suits one will simply not be seen by the other. Therefore, even with perfect logic of "one zip for both versions," you would have to put two copies of each controller into the archive. The package structure of both versions is described in detail in the articles on the anatomy of an extension and ocmod.zip.
Which strategy should you choose?
In practice, three approaches work:
- Two independent code branches. Simplest at the start, most expensive to maintain: every fixed bug must be transferred manually, and the branches gradually diverge.
- A shared core and two thin packages. All logic (calculations, requests to the service API, data building) lives in classes without OpenCart dependencies. The package for each version contains only controllers, models, templates, language files, and event registration that call the core. This is the recommended option for integration modules: payments, shipping, fiscalization, feeds.
- One code with an adapter. Controllers are identical, and the difference is hidden behind a thin adapter layer, plus a build that distributes files across two structures. Suitable for small modules, but the adapter quickly grows with exceptions.
What follows concerns the second approach, because it best survives years of support.
How to extract the shared core?
The shared core consists of regular PHP classes with their own namespace, which know nothing about the registryA shared container of objects ($this->config, $this->db). More in the glossary →, $this->config or $this->load. They receive everything they need through the constructor.
Example: a shared core class without OpenCart dependencies
<?php
namespace Mymod\Core;
class DeliveryCalculator {
private $client;
private $settings;
public function __construct(ApiClient $client, array $settings) {
$this->client = $client;
$this->settings = $settings;
}
public function quote(array $address, float $weight, float $total) {
if ($total >= (float)($this->settings['free_from'] ?? 0) && !empty($this->settings['free_from'])) {
return 0.0;
}
return $this->client->price($address['city'] ?? '', $weight);
}
}
Note: there are no typed properties, union return types, match, or other features of newer PHP here. If the module must work on 3.0.4.1, where the minimum PHP versionThe language version on the hosting. 3.0.4.1 requires at least PHP 7.3, while 3.0.5.1 and 4.1.0.4 require at least PHP 8.1; check extension compatibility before updating. More in the glossary → is 7.3, the core must compile on PHP 7.3. For a module only for 3.0.5.1 and 4.1, you can write for PHP 8.1.
Where to put these classes:
- OpenCart 3.0:
system/. Include vialibrary/ mymod/ core/ require_once(DIR_SYSTEM . 'library/or your own autoloader in this directory.mymod/ core/ DeliveryCalculator.php') - OpenCart 4.1:
extension/. The core registers autoloading for themymod/ system/ library/ core/ Opencart\System\Library\Extension\Mymodnamespace, but your namespaceMymod\Coreis different, so include it explicitly in the same way fromDIR_EXTENSION . 'mymod/.system/ library/ core/ '
Your own namespace (rather than Opencart\...) allows keeping byte-for-byte identical core files in both packages. They are copied by the build script, not by the developer by hand.
A thin layer for each version
The controller or model of a specific version only collects input data from OpenCart and passes it to the core:
Example: the 4.1 shipping model as a wrapper over the core
namespace Opencart\Catalog\Model\Extension\Mymod\Shipping;
class Mymod extends \Opencart\System\Engine\Model {
public function getQuote(array $address): array {
require_once(DIR_EXTENSION . 'mymod/system/library/core/autoload.php');
$settings = [
'free_from' => $this->config->get('shipping_mymod_free_from'),
'api_key' => $this->config->get('shipping_mymod_api_key')
];
$calculator = new \Mymod\Core\DeliveryCalculator(new \Mymod\Core\ApiClient($settings['api_key']), $settings);
$cost = $calculator->quote($address, $this->cart->getWeight(), $this->cart->getSubTotal());
// далі масив quote у форматі 4.1
return [];
}
}
In 3.0, the same file is catalog/ with the class ModelExtensionShippingMymod. The method name key there is title, and in 4.1 it is name, and the structure of the quote array is also different. These differences live in the thin layer, and the core knows nothing about them. The array format for both versions is analyzed in the article on the shipping module.
Which differences most often break porting?
Most of these differences don't produce errors but silently change behavior. It's worth keeping them in front of you as a checklist.
Events
- Method separator in a model trigger: slash in 3.0, dot in 4.1. A trigger with the wrong separator will not fire, and there will be no error.
- Method names have changed:
addOrderHistoryin 3.0 andaddHistoryin 4.1. - In 4.1, the handler's return value is ignored. Code from 3.0 that returned a value from
beforeto cancel the call will cancel nothing in 4.1. addEvent()takes separate arguments in 3.0 and an array in 4.1.
Details are in the articles on the event system and choosing between events and OCMOD. OCMOD exists formally in 4.1, but it is not worth porting 3.0 modifications to it: for new code in both branches, events are better.
Settings, models, and types
getSettingValue()in 3.0 returnsnull,getValue()in 4.1 returns''.getProduct()of the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → returnsfalsein 3.0 and[]in 4.1. Theif ($product_info)check works in both, while=== falseworks only in 3.0.$thisreturns the id in 3.0 and->customer ->isLogged() boolin 4.1.- A cache miss in the file driver is
falsein 3.0 and[]in 4.1. - Uninstalling a module in 3.0 removes permissions from all groups; in 4.1 it does not remove permissions.
Database schema
- Product codes: in 3.0 the columns
product.sku,upc,ean, etc.; in 4.1 a separate tableproduct_code. seo_url: the columnquery(product_id=1) in 3.0 and the pairkey/valuein 4.1.- Engine and collation: 3.0 is almost entirely MyISAM, in 3.0.4.1
utf8_general_ci, in 3.0.5.1utf8mb4_unicode_ci. 4.1 uses InnoDB andutf8mb4_unicode_ci. Create module tables on InnoDB withutf8mb4_unicode_cior withoutCOLLATE. A table withoutCOLLATEtakes the database collation, which does not necessarily match the collation of the core tables. In 3.0.4.1 (utf8_general_ci), a JOIN on text fields may still produceIllegal mix of collations, so join module tables with core tables by INT id.
Keep all SQL queries to core tables in the thin layer (models of a specific version), not in the shared core. The rules for module tables are described in the article on the module's own tables.
Language, Request, URL
- 3.0 loads
en-gband the active language on top of it. 4.1 loads only the active language, and a missing key is displayed as the key itself. A module that "worked fine" in 3.0 only with the English file will showtext_titleon the Ukrainian storefront in 4.1. Put complete language files for every language. Details are in the article on language files. - In 4.1 the Request has the helpers
get($key, $type)andpost($key, $type); 3.0 does not have them. Code that uses them will fail in 3.0. $this: the third parameter means HTTPS in 3.0 and "do not encode ampersands" in 4.1. Links on the 4.1 storefront usually carry the->url ->link() languageparameter.
Templates
Twig in both versions, but the admin markup is different: 3.0 uses Bootstrap 3, 4.1 uses Bootstrap 5, and the 4.1 form is saved via an AJAX request. Admin templates are not transferred between versions; they are written separately. Storefront templates should also be kept separate for each branch, because the standard themes of 3.0 and 4.1 differ in classes and blocks.
How to build and test the two packages?
A repository structure that works well:
Example: a module repository with two packages
core/ # спільні класи Mymod\Core, PHP 7.3+
oc3/upload/admin/... # тонкий шар 3.0
oc3/upload/catalog/...
oc3/install.xml # лише якщо потрібен OCMOD
oc4/admin/... # тонкий шар 4.1 (корінь пакета extension/mymod)
oc4/catalog/...
oc4/install.json
build.sh # копіює core/ у обидва пакети і пакує два zip
The build script copies core/ into oc3/ and oc4/, checks the syntax of all php -l files on the minimum PHP version of each branch, and packs two archives: one for 3.0 and one for 4.1. The archive for 4.1 must be named mymod.ocmod.zip, meaning the file name must match the extension code. The 4.1 installer takes the extension code from the archive name (without .ocmod.zip) and unpacks the package into extension/{код}/. An archive named mymod-oc4.ocmod.zip would end up in extension/, and then the namespaces Extension\Mymod and the path DIR_EXTENSION . 'mymod/ from the examples would not match the real directory, so the module would not load. Use different names like mymod-oc3/mymod-oc4 only for files downloaded from the site, and before installing in 4.1 the archive must be named mymod.ocmod.zip. The module version number is set from one place into both packages.
Testing matrix
- OpenCart 3.0.4.1 on PHP 7.3 or 7.4, if you support it;
- OpenCart 3.0.5.1 on PHP 8.1 and newer;
- OpenCart 4.1.0.4 on PHP 8.1 and newer;
- in each: installation, saving settings, the main storefront scenario, uninstallation, and reinstallation.
The core version in the code can be determined from the constant VERSION, which is defined by the storefront and admin index.php ('3.0.5.1', '4.1.0.4'). It is not needed in the shared core: if the core had to check the OpenCart version, then that logic belongs to the thin layer.
Summary
- One archive for 3.0 and 4.1 is practically impossible due to the different file structure, namespaces, and routes.
- The best approach is a shared core without OpenCart dependencies and two thin packages with controllers, models, templates, and events for each version.
- Write the core for the minimum PHP of the oldest supported branch (7.3 for 3.0.4.1).
- Keep a checklist of differences at hand: trigger separators, handler return values,
null/''/false/[],product_code,seo_url, and language files without theen-gbfallback in 4.1. - Build both archives with one script and test each branch on its minimum and current PHP versions.
Other materials on debugging, performance, and module code quality are collected in the section "Debugging, Performance, and Code Quality".