No results found

Try a different search query

Popular searches:

Add to Cart

Cart

You have no purchases yet

Browse Marketplace
Original Subtotal:
Product discounts:
Quantity Discount:
Total:
View Cart
Building extensions

ocmod.zip: how to package an OpenCart 3.0 and 4.1 module with install.xml and install.json

Structure of the .ocmod.zip archive for OpenCart 3.0 and 4.1: upload folder, install.xml, install.json, OCMOD files, updates, and common reasons why Installer rejects the archive.

9 min read OC 3.0 OC 4.1

Examples for 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 → 4.1; in 4.0.x, model triggers and event handling differ.

The ocmod.zip archive is uploaded via the ExtensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → → Installer page, but its internal structure differs between versions. In OpenCart 3.0, files go into the upload/ folder with the same directory structure as the store, and the modification is described in install.xml. In OpenCart 4.1, the root of the archive becomes the extension folder extension/{код}/, the module description is in install.json, and modifications are in the ocmod/ folder. Below we cover both schemes, the Installer rules, and the pitfalls when updating.

What should the archive look like for OpenCart 3.0?

Installer 3.0 accepts only a file whose name ends in .ocmod.zip, otherwise it shows "Invalid file type". After upload, the archive is extracted to a temporary directory, and two things are processed:

  • the upload/ folder: its contents are moved into the store directories, meaning upload/admin/ goes to admin, upload/catalog/ to the catalog, upload/system/ to system, and upload/image/ to image;
  • the install.xml file in the archive root: a OCMOD modification is created from it.

Example

mymodule.ocmod.zip
├── install.xml
└── upload/
    ├── admin/
    │   ├── controller/extension/module/mymodule.php
    │   ├── language/en-gb/extension/module/mymodule.php
    │   ├── model/extension/module/mymodule.php
    │   └── view/template/extension/module/mymodule.twig
    └── catalog/
        ├── controller/extension/module/mymodule.php
        └── view/theme/default/template/extension/module/mymodule.twig

You can't write files just anywhere. Installer 3.0 has a whitelist of directories and stops with the error "The directory … is not allowed to be written to" if a file falls outside them. The list includes:

  • admin/controller/extension/, admin/model/extension/, admin/view/template/extension/, admin/language/, as well as admin/view/image/, admin/view/javascript/, admin/view/stylesheet/;
  • catalog/controller/extension/, catalog/model/extension/, catalog/language/, catalog/view/javascript/, catalog/view/theme/;
  • system/config/, system/library/, image/catalog/.

So you cannot ship your own controllerCode that receives a request and prepares data. More in the glossary → in catalog/controller/product/ via the archive. If you need to change a core file, do it through a modification in install.xml or through an event, not by replacing the file. In 3.0, files are moved with overwriting: if a file with that path already exists, it will be replaced.

What should the archive look like for OpenCart 4.1?

In 4.1, extensions live separately from the core, and the archive reflects this directly. The file name without .ocmod.zip becomes the extension code and folder name: mymodule.ocmod.zip will be extracted to extension/mymodule/. Routes, namespaces, and permissions depend on this name, so the archive cannot be renamed after release.

Example

mymodule.ocmod.zip
├── install.json
├── admin/
│   ├── controller/module/mymodule.php
│   ├── language/en-gb/module/mymodule.php
│   ├── model/module/mymodule.php
│   └── view/template/module/mymodule.twig
├── catalog/
│   ├── controller/module/mymodule.php
│   └── view/template/module/mymodule.twig
├── system/
│   └── library/mymodule/client.php
└── ocmod/
    └── mymodule.ocmod.xml

When extracting, Installer 4.1 follows these rules:

  • everything from the archive root goes to extension/{код}/;
  • paths starting with image/ are copied to the store's shared image/ directory;
  • paths starting with system/storage/ are copied to the storage directory, for example for ComposerA PHP dependency manager. Some 4.1 extensions use it. More in the glossary → libraries;
  • the ocmod/ folder is not copied: a separate step reads the *.ocmod.xml files from it and writes them to the database as modifications;
  • items with .. in the path are skipped.

Installer records every file and directory it creates in the database. So uninstalling in Installer removes exactly what was installed from the archive, nothing extra. Files are extracted in batches of 200, so large archives install too, just over several requests.

«Розширення → Встановлення розширень»: завантажений архів і кнопка встановлення
Extensions → Installer: uploaded archive and install button · OpenCart 4.1.0.4

What should go into install.json and install.xml?

These files describe the extension for the administrator and for the database. Their fields only partly match.

Fieldinstall.xml (3.0)install.json (4.1)
Name<name>name
Code<code>, required and unique among modificationstaken from the archive name; the field in JSON does not affect the code
Version<version>version
Author<author>author
Link<link>link
Descriptionnonedescription

Example

{
  "name": "My Module",
  "version": "1.0.0",
  "author": "Studio Name",
  "link": "https://example.com/mymodule",
  "description": "Блок рекомендацій для сторінки товару"
}

OpenCart 4.1: without install.json in the archive root, Installer rejects the upload with the message "Could not find install.json!". Missing keys are filled with empty strings, so valid JSON with name and version is the minimum.

OpenCart 3.0: install.xml is not required, because a module without modifications doesn't need it. If the file is present but there is no <code>, installation stops with "Unique code is required for modification XML!", and a code already used by another modification gives "Modification … is already being used!". Build the code from your brand prefix and the module name.

In 4.1 there can be several modifications, one ocmod/{код}.ocmod.xml file for each. The modification code is taken from the file name. How to write the OCMOD instructions themselves (<file>, <search>, <add>) is described in detail in the blog article OCMOD in OpenCart: code changes without modifying system files.

«Розширення → Модифікації»: модифікація з архіву і кнопка «Оновити»
Extensions → Modifications: modification from the archive and the Refresh button · OpenCart 4.1.0.4

After installing an archive with a modification, the administrator must click Refresh (Оновити) on the Extensions → Modifications page (in 3.0 it is Extensions → Modifications, in 4.1 Extensions → Modification; route marketplace/modification in both branches), otherwise the changes won't be applied. Mention this in the module's instructions.

How do you build the archive without surprises?

Build the archive with a script, not manually from a file manager. This way every release has the same structure, and nothing extra gets into it.

Example

# OpenCart 4.1: пакуємо вміст каталогу, а не сам каталог
cd build/mymodule
zip -r ../../mymodule.ocmod.zip . -x "*.DS_Store" -x "__MACOSX/*" -x ".git/*"

# OpenCart 3.0: у корені архіву лише upload/ і install.xml
cd build/oc3
zip -r ../../mymodule-oc3.ocmod.zip upload install.xml -x "*.DS_Store"

Before publishing, check the contents with the unzip -l mymodule.ocmod.zip command. The top level must contain install.json, admin/, catalog/ in 4.1, or upload/ and install.xml in 3.0.

  • File name. In 4.1 it becomes the code, so use only lowercase Latin letters, digits, and underscores, with no version in the name. An archive named mymodule-1.2.ocmod.zip will create the folder extension/mymodule-1.2/, and the next version becomes a separate extension. Moreover, the core builds a PHP namespace from the folder name: it removes underscores but keeps hyphens and dots, and such a namespace becomes invalid.
  • Service files. .git, node_modules, tests, SCSS sources: all of this will end up on the client's server. Exclude them via -x.
  • Encoding. Save PHP files as UTF-8 without BOM. A BOM before <?php causes "headers already sent" and breaks JSON responses.
  • Two archives for two versions. The structures are incompatible, so build separate archives for 3.0 and 4.1 and label them in the file name on the marketplace, not inside the extension code.

How do you release module updates?

Most questions come up with updates, because Installer behaves oppositely in the two versions.

OpenCart 3.0: re-uploading the archive overwrites the files, so an update is essentially a new upload. But files you removed from the new version will stay on the server and must be deleted separately. A modification with the same <code> is considered taken, so before updating, delete the old modification on the Modifications page.

OpenCart 4.1: Installer does not overwrite existing files, and installation stops if the extension/{код}/ directory already exists. You also can't delete files through Installer while at least one extension from that folder is installed in Extensions → Extensions: Installer will answer that they must be removed first. So the standard admin update path is:

  1. In Extensions → Extensions, uninstall each type the module ships. This runs uninstall(), and the core deletes the settings from setting.
  2. In Extensions → Installer, uninstall the installed version. The files, directories, and modifications of this archive are removed according to the recorded list. Then, on the same page, delete the archive itself from the list: otherwise it will remain in storage/marketplace/, and uploading a new file with the same name will fail with "already exists".
  3. Upload the new archive with the same name, install it, and install the module again in Extensions → Extensions.
  4. Click Refresh in Modifications if the module has OCMOD, and save the settings again.

This path loses settings, so account for it when designing the module. Keep data that must survive the update (history, bindings, logs) in your own tables and don't delete them in uninstall(). The alternative is updating over the files via SFTP or deployment: settings remain, but Installer won't know about the new files. In both cases, add new columns and events via migrations, as described in the article install and uninstall of a module.

Checklist before publishing the archive

  1. The archive is named {код}.ocmod.zip; in 4.1 without a version and without hyphens in the code.
  2. The top level of the archive matches the version: upload/ and install.xml for 3.0, the extension folder contents and install.json for 4.1.
  3. In 3.0, all files are in directories from the Installer whitelist.
  4. install.xml has a unique <code>, and install.json is valid JSON (check via php -r 'var_dump(json_decode(file_get_contents("install.json")));').
  5. The archive is installed on a clean store of the correct version, the module is installed with the Install button, settings are saved, and the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → is checked.
  6. The module is uninstalled and installed again: no errors about existing files or modification codes.

The general structure of files that go into the archive is described in the article anatomy of an extension in 3.0 and 4.1. A complete working example is in the material how to create a module from scratch.

Summary

  • OpenCart 3.0: files go into upload/ with a directory whitelist, the modification is in install.xml with a required unique <code>, and on re-upload files are overwritten.
  • OpenCart 4.1: the archive root becomes extension/{назва архіву}/, install.json is required, modifications are in ocmod/*.ocmod.xml, and existing files are not overwritten.
  • In 4.1 the archive name is the extension code: stable, in Latin letters, without a version.
  • In 4.1, updating means uninstalling in Installer and installing anew, and database changes require migrations.

Other topics about extensions are collected on the page creating extensions for OpenCart.

FAQ

Why does Installer say the file already exists when the module has been deleted?
In 4.1, the uploaded archive stays in storage/marketplace until you remove it from the Installer list. Delete the archive entry, not just the installed extension.
Can I install a module without Installer, just by copying the files?
Yes. In 3.0, the files are copied to the same paths as in upload/, and in 4.1 to extension/{код}/. However, OCMOD modifications won't be created automatically, and Installer won't know about these files.
Can I make one archive for both 3.0 and 4.1?
No, the archive structures and file paths are incompatible. Make two separate archives with shared business logic in a library.
What should I do if the hosting limits the upload size?
Installer depends on upload_max_filesize and post_max_size in PHP. Reduce the archive size by removing service files, or ask your hosting provider to raise the limit.
Was this article helpful?
Thanks for your feedback!

Related articles

Didn't find the answer?

Ask the community: OpenCart developers answer on the forum.

Ask on the forum
We use cookies

We use cookies and similar technologies to improve your experience, analyse traffic, and show personalised ads. Read our Cookie Policy for details.