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
Events and OCMOD

OCMOD in OpenCart 4.1: What Remains, How It Works, and What to Use Instead

OCMOD in OpenCart 4.1 still exists, but it works differently than in 3.0: a different archive format, copies in extension/ocmod, and route overrides handled by events. Here's what it still does and when events are the better choice.

8 min read OC 4.1

OCMOD in 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 has not disappeared: 4.1 has a modifications page, the same search/add syntax, and a «Refresh» button. But modified copies are now stored in extension/ocmod/, and the core substitutes them through service events, so a modification only affects what is loaded through the Loader: controllers, models, templates, and some libraries. For new module code, the main tool in 4.1 is events, while OCMOD remains for targeted edits that events cannot handle.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ. The operation syntax is covered in detail in the article OCMOD syntax in OpenCart 3.0. In 4.1 it is the same; below are only the differences.

What remains of OCMOD in OpenCart 4.1?

The page «ExtensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → → Modifications» (Extensions → Modifications), route marketplace/modification, is present in 4.1. From it you can enable, disable, or delete a modification, click «Refresh» and «Clear», and view the ocmod.log log.

The logic for applying operations in the refresh() method almost literally repeats 3.0:

  • a regular search searches for text in a single line, case-insensitively, and is trimmed of whitespace by default;
  • position accepts the values replace, before, and after, and an unknown value works as replace;
  • the attributes offset, index, and trim, the regex="true" mode with limit, the ignoreif element, and the error attributeA product characteristic that does not affect the price: material, weight, power. It is set in "Catalog → Attributes" and displayed on the "Specification" tab. More in the glossary → (skip, abort, or stopping the remaining operations in the block) behave the same way;
  • during the refresh, maintenance mode is turned on, and at the end the previous value is restored.

This means a modification written for 3.0 does not have to be rewritten syntactically in 4.1. What does have to be rewritten is what it searches for: paths, namespaces, method names, and template markup are different in 4.1.

Extensions → Modifications: the «Refresh» button, modification status, and log
Extensions → Modifications: the «Refresh» button, modification status, and log · OpenCart 4.1.0.4

What changed in 4.1 compared with 3.0?

ItemOpenCart 3.0OpenCart 4.1
File in the archiveinstall.xml in the rootocmod/*.ocmod.xml files
Modification code<code> element, requiredfile name without .ocmod.xml
Reinstalling with the same codethe old record is replacedthe new XML is skipped, the old one remains
Status after installationenableddisabled, must be enabled and refreshed
Where copies are writtenstorage/modification/extension/ocmod/
Prefixes in pathcatalog/, admin/, system/the same plus extension/
How the core picks up the copymodification() function in includeevent/modification.* events with priority 0
Service system/modification.xmlpresentabsent

The most unpleasant difference in practice concerns reinstallation and status. The 4.1 installer (marketplace/installer) creates a modification record with status 0. After installing the module, nothing changes until the administrator enables the modification and clicks «Refresh». And if you update a module and a modification with that code already exists, the installer will not write the new XML: the old modification must be deleted manually before installing the new version.

The location where files are unpacked has also changed. The contents of a 4.1 archive are copied into extension/{код}/, not into the core directories, and the ocmod/ folder from the archive is not copied at all: its XML files go only into the database. For how to build such an archive with install.json, see the article ocmod.zip for 3.0 and 4.1.

Example

<?xml version="1.0" encoding="utf-8"?>
<modification>
  <!-- архів my_badge.ocmod.zip, файл ocmod/my_badge.ocmod.xml -->
  <name>My badge</name>
  <description>Бейдж на картці товару</description>
  <version>1.0.0</version>
  <author>Developer</author>
  <link>https://example.com</link>
  <file path="catalog/view/template/product/product.twig">
    <operation error="skip">
      <search><![CDATA[<div id="product">]]></search>
      <add position="before"><![CDATA[{% if my_badge %}<span class="badge">{{ my_badge }}</span>{% endif %}]]></add>
    </operation>
  </file>
</modification>

How does 4.1 substitute modified files, and what comes of it?

In 3.0, modified copies were included at the level of include. 4.1 has no such mechanism. Instead, service handlers with priority 0 are registered in system/config/catalog.php and admin.php: event/modification.controller, .model, .view, and .library. Each checks whether a modified copy exists for the current route and, if so, adds the prefix extension/ocmod/ to the route. To do this, startup/extension registers the namespaces …\Extension\Ocmod and the template paths to extension/ocmod/.

This defines the limits of OCMOD in 4.1:

  • Twig templates are picked up reliably: the handler checks for the file's presence in extension/ocmod/catalog/view/template/ and switches the route. But, as in 3.0, a version of the template saved in «Design → Theme Editor» is substituted in $code with priority 500 and overrides the OCMOD edit.
  • Controllers and models are picked up only when they are called through the Loader or as the main page route. The handler looks for the class in the namespace Opencart\Catalog\Controller\Extension\Ocmod\…, but the copy keeps the original namespace, because refresh() does not change it. We verified this on the 4.1.0.4 autoloader: the class check returns false, the route is not switched, but the autoloader has already included the copy's file, and it defines the class under the original name. So the modified version usually works, but it takes effect indirectly, and the behavior should be checked on a test store.
  • Startup controllers run directly, without events, so their modifications do not take effect.
  • system/engine/ and system/framework.php files, and libraries created via new (for example customer and currency on the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →) do not go through the Loader, and OCMOD has no effect on them. Libraries loaded via $this->load->library(), such as the cart in startup/application, go through the library/*/before event.
  • Language files. The config refers to event/modification.language, but the event/modification controllerCode that receives a request and prepares data. More in the glossary → in release 4.1.0.4 has no language method, so modified language files are not picked up. It is simpler to change texts through «Design → Language Editor» or via an event.

What to replace OCMOD with in a module for 4.1?

Most typical OCMOD 3.0 tasks are solved in 4.1 with events, and this code is more reliable: it does not depend on the exact text of a core line and does not break when a third-party theme is updated. Equivalents:

TaskHow it was done with OCMODHow to do it in 4.1
Pass a variable to the template$data[...] line in the controllerview/…/before, add the key in $data
Insert a block into the markup.twig editview/…/before with an $code edit, or OCMOD on the template
Change product or list dataSQL or array edit in the modelmodel/…/after, $output edit
React to an order status changeinsertion into addOrderHistory()catalog/model/checkout/order.addHistory/after
Admin menu itemcolumn_left.php editadmin/view/common/column_left/before, add the element in $data['menus']
Change the HTML of a finished pagetemplate editview/…/after, $output edit

Example

// OpenCart 4.1, тригер admin/view/common/column_left/before
public function menu(string &$route, array &$data, string &$code, string &$output): void {
	if (!$this->user->hasPermission('access', 'extension/my_module/module/my_module')) {
		return;
	}

	$data['menus'][] = [
		'id'       => 'menu-my-module',
		'icon'     => 'fas fa-star',
		'name'     => 'My module',
		'href'     => $this->url->link('extension/my_module/module/my_module', 'user_token=' . $this->session->data['user_token']),
		'children' => []
	];
}

Check the menu element structure against admin/controller/common/column_left.php of your version: the template expects the same keys that the controller generates. For details on template and model events, see the articles changing a template via view/*/before and intercepting models via model/*/after. Event registration in install() is described in the article addEvent and handler order.

When is OCMOD still justified in 4.1?

Events change input and output, but not the logic inside a method. OCMOD makes sense when you need to:

  • change a condition in an SQL query of a core model that cannot be fixed either through $args or through $output without a repeated query;
  • insert markup into a template when neither an $code edit nor a full template replacement suits you;
  • quickly fix a bug in a third-party extension's code from extension/ without changing its files (the prefix extension/ in path appeared precisely in 4.1).

The workflow in these cases:

  1. During development, place the XML in system/ with the name *.ocmod.xml: such files are applied on «Refresh» without installing an archive.
  2. Click «Refresh» and check the log: the lines MOD:, FILE:, LINE:, and the absence of NOT FOUND.
  3. Make sure the copy appeared in extension/ocmod/… and that the change is visible on the site.
  4. Move the XML into the ocmod/{код}.ocmod.xml of the module archive, and remove the file from system/.
  5. In the module instructions, state that after installation the modification must be enabled and «Refresh» clicked.

Also keep in mind that refresh() in 4.1 does not check for the presence of <search> and <add> in an operation or of the root <modification>: broken XML will stop the refresh with an error, and the store may remain in maintenance mode until you turn it off in the settings. For a broader view of why OCMOD exists and how it is used, see the blog: OCMOD in OpenCart: code edits without changing system files.

Summary

  • OCMOD in 4.1 is alive: the same operation syntax, the «Extensions → Modifications» page, «Refresh», and ocmod.log.
  • The archive contains ocmod/{код}.ocmod.xml; the code is taken from the file name, the modification is installed disabled, and an existing one with the same code is not overwritten.
  • Copies are written to extension/ocmod/ and picked up by the event/modification.* events, that is, only for what goes through the Loader; templates work reliably, language files in 4.1.0.4 do not.
  • For variables, model data, menus, and HTML in 4.1, use events; keep OCMOD for logic changes inside methods.

How to choose between events and OCMOD for a specific module, and how to find a modification that was not applied, is covered in separate articles in the section Events and OCMOD. The general mechanics of events are described in the article the OpenCart event system.

FAQ

Can You Install a Modification in 4.1 Without a Module, as a Separate Archive?
Yes, a .ocmod.zip archive can contain only install.json and an ocmod folder with XML files. The installer will create an extension record, but the modification will need to be enabled and refreshed.
Where to Find Modified Files in 4.1 If They Are Not in storage/modification?
In 4.1, copies are written to the extension/ocmod/ directory with the same structure: catalog/, admin/, system/ or extension/{code}/.
Do vQmod Modifications Work in 4.1?
No, the OpenCart 4.1 core supports only OCMOD; it does not read vQmod files.
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.