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

Events or OCMOD: Which to Choose for an OpenCart 3.0 and 4.1 Module

Comparison of events and OCMOD for OpenCart 3.0 and 4.1 modules: what each mechanism can do, how it breaks, what happens to it when the core is updated, and how to choose for a specific task.

8 min read OC 3.0 OC 4.1

Events or OCMOD is a question every module that needs to change the behavior of the OpenCart core has to answer. The short answer: if the task can be solved by changing input data, the result, or a template, choose events. Keep OCMOD for edits to the logic inside a core method that events cannot reach. In 4.1 this boundary shifts even further toward events, because OCMOD there applies only to some of the files.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ. Each tool's mechanics are described separately: the OpenCart event system and the OCMOD syntax in OpenCart 3.0.

How do events and OCMOD fundamentally differ?

Both mechanisms let you avoid editing core files, but they work at different levels.

Events are extension points that the core calls itself. The loader, before and after a controllerCode that receives a request and prepares data. More in the glossary →, model, template, or language fileA PHP file with interface texts, separate for each language. More in the glossary →, calls the registered handlers and passes them arguments by reference. Your code lives in a separate module controller and receives data as structures: the template's $data array, the product array, the order id. You do not know, and do not need to know, how the code inside the core method is written.

OCMOD is search and replace of text in files. When you click "Refresh" on the modifications page, the core takes the original file, searches it for a line containing <search>, inserts your fragment, and saves a copy: in 3.0 into storage/modification/, in 4.1 into extension/ocmod/. From then on, the copy is what runs. You work with the source code as text and can change anything written in it, but you are bound to specific lines.

Everything else follows from this difference:

  • an event breaks when the contract changes (method name, set of arguments, array keys), while OCMOD breaks when the text changes (indentation, line order, variable name);
  • an event works right after the event table is written, while OCMOD works only after "Refresh";
  • several modules on one event run in turn according to sort_order, while several OCMODs on one file search for lines in text already changed by the previous modifications.

How do they compare in capabilities?

CriterionEventsOCMOD
Change data for the templateyes, view/…/beforeyes, by editing the controller
Change template markup4.1: yes, via &$code in view/…/before or by substituting the template; 3.0: yes, by editing $output in view/…/after (by returning a value)yes, by editing .twig
Change the model resultyes, model/…/afteryes
Change an SQL condition inside a methodnoyes
Cancel a method call3.0: yes, by returning a value; 4.1: only for templates via $outputyes
Change startup controllers, system/engineno3.0: yes; 4.1: no
Change language filesyes, language/…/after3.0: yes; 4.1.0.4: no
React to database writesyes, after-events add*, edit*yes, by inserting into a method
Administrator action needed after installationno3.0: "Refresh"; 4.1: enable and "Refresh"

The table shows the main point: events are enough for most tasks such as "add a field," "show a block," "change the price on the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →," or "send data to a CRM." OCMOD is indispensable only where you must intervene in the middle of an algorithm or in code that does not pass through the loader.

A note on 4.1. There, OCMOD applies to controllers, models, templates, and libraries loaded through the Loader, because the core substitutes copies through service events event/modification.*. Startup controllers, system/engine/ classes, and libraries created via new cannot be changed with OCMOD in 4.1. For details, see the article OCMOD in OpenCart 4.1.

What is more reliable during updates and with third-party themes?

Most often a module breaks not in your store but in the buyer's: with a different theme, with a dozen other modules, and with a different core version. This is where the difference between the tools is most noticeable.

Third-party themes

OCMOD on a template searches for a specific line of markup. If the theme has its own copy of the template with different markup, the line will not be found: with error="skip" the edit will be silently skipped, and without it, the following operations of the block will also be cancelled. An event with $data does not depend on markup at all. An event with an $code edit depends on it just like OCMOD does, so for themes use the least fragile technique that suits the task.

Conflicts with other modules

Two OCMODs that search for the same line often interfere with each other: the first changes the line, and the second can no longer find it. Only ocmod.log helps sort this out. Two events on the same point simply run in turn; the order is controlled by sort_order. The exception is 3.0, where a handler that returns a value stops the rest, so module handlers should not return anything unless necessary.

Core updates

Between 3.0.4.1 and 3.0.5.1, certain core files changed, and OCMOD tied to their text could stop finding lines. Events survive such updates better, as long as method names and the set of arguments are preserved. Moving between the 3.0 and 4.1 branches is not survived by anything: namespaces change, trigger separators (/ versus .), method names (addOrderHistory versus addHistory), and the templates themselves.

Visibility for the administrator

A module's events are visible in ExtensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → → Events, where each one can be disabled separately. Modifications are in Extensions → Modifications. An important difference: after an event is disabled, its effect disappears from the next request, while a disabled modification continues to apply until someone clicks "Refresh."

Extensions → Events: each module event can be disabled separately, without "Refresh"
Extensions → Events: each module event can be disabled separately, without "Refresh" · OpenCart 4.1.0.4

How do you choose the tool for a specific task?

flowchart TD
A["What needs to change?"] --> B{"Data for the template, model result, or controller?"}
B -- "yes" --> E["Event: view, model, or controller"]
B -- "no" --> C{"Template markup?"}
C -- "yes" --> F["Event with $data; markup: 4.1 — $code in view before or template substitution, 3.0 — edit $output in view after; OCMOD on twig as a fallback"]
C -- "no" --> D{"Logic inside a method or startup?"}
D -- "yes" --> G["OCMOD (in 4.1 only for files via Loader)"]
D -- "no" --> H["Your own controller, model, or extension type"]

Practical equivalents for typical tasks:

  • A new field on the product card. An event catalog/model/catalog/product/getProduct/after (3.0) or catalog/model/catalog/product.getProduct/after (4.1) for the data, and catalog/view/product/product/before for the template. A trigger with the wrong separator before the method will not fire, and no error will be shown. See model interception and changing a template with an event.
  • An analytics script on all pages. An event view/common/header/after with an $output edit, or a variable in common/header.
  • An action when the order statusA stage of processing: "Pending," "Shipped," "Complete." Custom statuses are added in the dictionary. More in the glossary → changes. An event on addOrderHistory (3.0) or addHistory (4.1).
  • A new page or AJAX endpoint. Neither events nor OCMOD: the module's own controller.
  • A discount the buyer will actually pay. A total-type module, not an event on the product price: the cart reads prices directly from the database. See OpenCart module types.
  • A different filter condition in the SQL of a core model. OCMOD, if an event on $args does not give the required effect.

Can events and OCMOD be combined in one module?

Yes, and it is often the best option: everything possible is done with events, and OCMOD is limited to one or two point operations. This combination offers several advantages:

  • fewer search operations means fewer chances that something will not be found on someone else's theme;
  • the main logic lives in ordinary PHP classes of the module, which are easy to read and debug;
  • if a modification was not applied, the module partly works instead of breaking completely.

A few rules for such a module:

  1. Register events in install() and remove them in uninstall() via deleteEventByCode(): the core does not remove the module's events by itself. See addEvent and handler order.
  2. For OCMOD operations, use error="skip" if they are independent, and ignoreif if the line might have been inserted earlier.
  3. Do not duplicate one change with two mechanisms: if a variable reaches the template through an event, do not also insert it into the controller with OCMOD.
  4. State in the instructions that after installation you must click "Refresh" on the modifications page, and in 4.1 also enable the modification.
  5. Check the module's status in every event handler: a module disabled in its settings does not disable its own events.

Summary

  • Events change the input and output of controllers, models, templates, and language files, do not depend on the text of core code, and take effect immediately after registration.
  • OCMOD changes the text of any line, but depends on exact markup and code, requires "Refresh," and conflicts with other modifications of the same lines.
  • In 4.1, OCMOD applies only to what goes through the Loader and is installed disabled, so events are the main tool there.
  • The best strategy for a module: do everything possible with events; use OCMOD point-wise for logic inside methods.

If a modification still was not applied, the section Events and OCMOD has a separate article on finding the cause. Packaging a module with events and a modification is described in the article ocmod.zip for 3.0 and 4.1, and a broader look at OCMOD is in the blog: OCMOD in OpenCart: code edits without changing system files.

FAQ

Are events slower than OCMOD?
Events alone only add iterating over registered handlers and calling methods, which is barely noticeable on a typical page. A handler with a heavy query that fires many times becomes noticeable, for example on every template via view/*/before.
Can an event change another module's file?
Yes, if that module loads its controllers, models, or templates through Loader: the trigger is built from their route just as it is for the core. Code that the module includes via include or new cannot be intercepted with events.
What do marketplaces choose for OpenCart 4 modules?
Check the specific marketplace's requirements in its rules. The common practice for 4.1 is events as the main mechanism, with OCMOD only where it cannot be avoided.
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.