# OpenCart 3 and 4 Module Compatibility: Supporting 3.0 and 4.1 with One Codebase

> Compatibility strategies for OpenCart 3 and 4 modules: a shared core and two thin packages, an adapter, a list of 3.0 and 4.1 differences that break migration, and how to test both branches.

Джерело: https://opencartcode.com/en/wiki/dev-practice/opencart-module-compatibility-3-4
OpenCart: 3.0, 4.1
Оновлено: 2026-11-25

Compatibility of an [OpenCart](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#opencart) 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/module/mymod/save` | dot: `extension/mymod/module/mymod.save` |
| Event triggers | `.../order/addOrderHistory/after` | `.../order.addHistory/after` |
| 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 [controller](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#controller) 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](https://opencartcode.com/en/wiki/dev-extensions/opencart-module-structure) and [ocmod.zip](https://opencartcode.com/en/wiki/dev-extensions/ocmod-zip).

## Which strategy should you choose?

In practice, three approaches work:

1. **Two independent code branches.** Simplest at the start, most expensive to maintain: every fixed bug must be transferred manually, and the branches gradually diverge.
2. **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.
3. **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 [registry](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#registry), `$this->config` or `$this->load`. They receive everything they need through the constructor.

Example: a shared core class without OpenCart dependencies

```php
<?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 version](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#php-version) 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/library/mymod/core/`. Include via `require_once(DIR_SYSTEM . 'library/mymod/core/DeliveryCalculator.php')` or your own autoloader in this directory.
- **OpenCart 4.1:** `extension/mymod/system/library/core/`. The core registers autoloading for the `Opencart\System\Library\Extension\Mymod` namespace, but your namespace `Mymod\Core` is different, so include it explicitly in the same way from `DIR_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

```php
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/model/extension/shipping/mymod.php` 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](https://opencartcode.com/en/wiki/dev-flows/opencart-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: `addOrderHistory` in 3.0 and `addHistory` in 4.1.
- In 4.1, the handler's return value is ignored. Code from 3.0 that returned a value from `before` to 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](https://opencartcode.com/en/wiki/dev-events-ocmod/events-opencart) and [choosing between events and OCMOD](https://opencartcode.com/en/wiki/dev-events-ocmod/events-ci-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 returns `null`, `getValue()` in 4.1 returns `''`.
- `getProduct()` of the [storefront](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#storefront) returns `false` in 3.0 and `[]` in 4.1. The `if ($product_info)` check works in both, while `=== false` works only in 3.0.
- `$this->customer->isLogged()` returns the id in 3.0 and `bool` in 4.1.
- A cache miss in the file driver is `false` in 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 table `product_code`.
- `seo_url`: the column `query` (`product_id=1`) in 3.0 and the pair `key`/`value` in 4.1.
- Engine and collation: 3.0 is almost entirely MyISAM, in 3.0.4.1 `utf8_general_ci`, in 3.0.5.1 `utf8mb4_unicode_ci`. 4.1 uses InnoDB and `utf8mb4_unicode_ci`. Create module tables on InnoDB with `utf8mb4_unicode_ci` or without `COLLATE`. A table without `COLLATE` takes 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 produce `Illegal 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](https://opencartcode.com/en/wiki/dev-extensions/opencart-module-tables).

### Language, Request, URL

- 3.0 loads `en-gb` and 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 show `text_title` on the Ukrainian storefront in 4.1. Put complete language files for every language. Details are in the article on [language files](https://opencartcode.com/en/wiki/dev-mvc/opencart-language-files).
- In 4.1 the Request has the helpers `get($key, $type)` and `post($key, $type)`; 3.0 does not have them. Code that uses them will fail in 3.0.
- `$this->url->link()`: 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 `language` parameter.

### 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

```bash
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/upload/system/library/mymod/core/` and `oc4/system/library/core/`, 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/mymod-oc4/`, 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.

> Before deciding to support both branches, look at which versions your customers use. A module for 4.1 without 3.0 support, or vice versa, is often cheaper to develop and more reliable than a universal one, and the difference in audience can be covered by a separate release later.

## 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 the `en-gb` fallback 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"](https://opencartcode.com/en/wiki/dev-practice).

## FAQ

**Can I install a 3.0 module archive on OpenCart 4.1?**

The installer won't accept every such archive, and even if the files unpack correctly, 4.1 won't find 3.0 controllers because it looks up classes in the extension package by namespace. 4.1 needs a separate package.

**Should I support OpenCart 4.0.x alongside 4.1?**

Model triggers and event handling work differently in 4.0.x, so it's effectively a third branch with its own testing. If there are few such customers, it's more honest to list only 4.1 in the module description.

**How do I migrate module settings when moving a store from 3.0 to 4.1?**

Keys in the setting table can usually stay the same if the group code (module_mymod, shipping_mymod) hasn't changed. Migrate the rest of the module data with migrations for your own tables.

**Can I use Composer in the shared core?**

Yes, but put the dependencies inside the module package and load them with its own autoload.php. Don't put them in the core's storage/vendor: that directory belongs to OpenCart itself, and another module or a core update may bring in a different version of the same library.
