OCMOD syntax 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 → 3.0 is an XML file that describes, for each core file, which line to find (<search>) and what to do with it (<add>). OpenCart does not change the core files themselves. After you click "Refresh", it builds modified copies in storage/ and includes them in place of the originals. Below are the exact search and replace rules from the code admin/, because that is where the causes of most "the modification didn't work" problems are hidden.
How does OCMOD work in OpenCart 3.0?
Modifications are not applied on every request. They are applied once, in the method refresh() of the controllerCode that receives a request and prepares data. More in the glossary → marketplace/, when the administrator clicks "Refresh" on 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 → → Modifications page. The process is as follows:
- During the refresh, maintenance mode is enabled (
config_maintenance), and the previous value is restored at the end. - The entire contents of the modifications directory (
DIR_MODIFICATION, by defaultstorage/) are deleted.modification/ - The XML files are collected: first
system/, then all files inmodification.xml system/*.ocmod.xml, then the enabled entries of themodificationtable. - For each XML in turn, operations are performed on the file contents in memory. If several modifications change the same file, the next one works with the result of the previous one.
- Only files whose contents have changed are written to
storage/, and the log is appended tomodification/ ocmod.login the logs directory.
How does the core pick up these copies? Through the same mechanism. The file system/ is a regular core modification: it uses a regex to wrap require and include in the loader, action, config, and language classes into a function modification() with system/startup.php, and it adds reading of the modified templates to system/. The function modification() returns the path to the copy in storage/ if it exists, or the path to the original.
One more detail for templates: the modified Twig copy is read only when no ready-made code is passed to the renderer. If the templateA display file: in 2.x it is .tpl, and in 3.0 and 4.1 it is .twig. More in the glossary → is saved in Design → Theme Editor, the handler event/theme will substitute the code from the database, and the OCMOD edit for that template will not be visible. For the directories storage, see the article storage directory in OpenCart.

How is a modification file structured?
In the OCMOD module, it is located in the file install.xml at the root of the archive .ocmod.zip. The installer reads name, code, author, version, and link from it and stores the entire XML in the modification table. The code element is required: without it, the installation stops with the error "Unique code is required for modification XML!". If a modification with this code already exists, the old record is deleted and replaced with the new one, so updating the module does not create duplicates. For how to package the archive, see the article ocmod.zip for 3.0 and 4.1.
Example
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>My badge</name>
<code>my_badge</code>
<version>1.0.0</version>
<author>Developer</author>
<link>https://example.com</link>
<file path="catalog/controller/product/product.php">
<operation error="skip">
<search><![CDATA[$data['model'] = $product_info['model'];]]></search>
<add position="after"><![CDATA[ $data['my_badge'] = $this->config->get('module_my_badge_text');]]></add>
</operation>
</file>
<file path="catalog/view/theme/*/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>
Rules for the path 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 →:
- The path must begin with
catalog/,admin/, orsystem/. Other prefixes are ignored without any message, so you cannot modify the rootindex.php, for example. - Multiple paths are separated by the
|character. - The path is expanded via
glob()withGLOB_BRACE:*and a list in curly braces work, for examplecatalog/. An asterisk in the theme path (as in the example) lets you target all installed themes that contain such a file.controller/ {product,checkout}/ *.php - If no file is found for the path, the operations are simply not performed.
Wrap the contents of search and add in CDATA, otherwise the characters <, >, and & will break the XML.
How do search and add work?
The most important OCMOD rule in 3.0: search works line by line. The file is split into lines, and each line is checked separately for a match of the text from search.
admin/ · OpenCart 3.0.5.1 · lines 295–304
if (stripos($line, $search) !== false) {
// If indexes are not used then just set the found status to true.
if (!$indexes) {
$match = true;
} elseif (in_array($i, $indexes)) {
$match = true;
}
$i++;
}
The consequences are:
- A multi-line
searchwill never be found. Search for one distinctive line, and for more complex cases use regex. - Search is case-insensitive (
stripos), and the match can be part of a line. But the replacement forposition="replace"is done viastr_replace, which is case-sensitive. Therefore the case insearchmust match the file exactly. Otherwise the line will be found (LINE:will appear in the log), but the replacement will silently not happen. - The
searchtext is trimmed of spaces if thetrimattribute is not set or istrue. So indentation in the search line does not matter. - The
addtext is, on the contrary, not trimmed unless you explicitly specifytrim="true". Indentation and line breaks from CDATA go into the file as they are. - Without
index, all matches in the file are changed. Theindexattribute accepts match numbers separated by commas, counting from zero:index="0"means only the first,index="0,2"means the first and third.
The position attribute in add has only three values:
| position | What happens | offset |
|---|---|---|
replace | in the found line, the text search is replaced with add | positive: removes N more lines after the found one; negative: N lines before it |
before | the add lines are inserted before the found line | insertion N lines higher |
after | the add lines are inserted after the found line | insertion N lines lower |
There are no top, bottom, ireplace, or similar values in OCMOD 3.0. An unknown value does not produce an error: in switch, the default branch is grouped together with replace, so a wrongly written position will silently work as a replacement. Likewise, a missing position attribute also becomes a replacement.
When is regex needed and how does it work?
If you add the search attribute to regex="true", the logic changes completely: instead of iterating over lines, preg_replace() is applied to the entire contents of the file. This is the only way to match several lines at once or to replace a fragment taking context into account.
Example
<operation error="skip">
<search regex="true" limit="1"><![CDATA[~(\$data\['tab_review'\]\s*=\s*)~]]></search>
<add><![CDATA[$data['my_tab'] = $this->language->get('text_my_tab');
$1]]></add>
</operation>
Features of regex mode in 3.0:
- The contents of
searchare a full PCRE expression with delimiters and modifiers, as forpreg_match(). The core uses the delimiter~so that slashes in paths don't need escaping. - Back-references to groups
addand$1work in$2; the attributesposition,offset,index, andtrimare ignored. - Both
searchandaddare always trimmed of spaces at the edges. - The
limitattribute limits the number of replacements; without it, all matches are changed. - An error in the expression is dangerous:
preg_replace()then returnsnull, and the contents of the file in memory become empty. Subsequent operations will find nothing, and an empty copy of the file may be written tostorage/. Test the expression separately, for example inmodification/ php -r, before clicking "Refresh".
Regex is convenient, but it breaks from the slightest formatting change in the file. For a single line, a regular search is more reliable and easier to understand when reading the log.
What do error and ignoreif do?
The error attribute of the operation element defines what to do if the search finds nothing:
admin/ · OpenCart 3.0.5.1 · lines 379–399
if (!$status) {
// Abort applying this modification completely.
if ($error == 'abort') {
$modification = $recovery;
// Log
$log[] = 'NOT FOUND - ABORTING!';
break 5;
}
// Skip current operation or break
elseif ($error == 'skip') {
// Log
$log[] = 'NOT FOUND - OPERATION SKIPPED!';
continue;
}
// Break current operations
else {
// Log
$log[] = 'NOT FOUND - OPERATIONS ABORTED!';
break;
}
}
| error | Behavior |
|---|---|
skip | skip only this operation and move on to the next one |
abort | roll back the changes of this modification and stop processing the rest of the XML: all modifications queued after it will also not be applied |
| anything else or empty | do not perform the remaining operations of this file for the current file |
The log value, which often appears in third-party modifications, is not handled separately and behaves as the default option. That means one unfound operation silently cancels all subsequent operations in this <file> block. If the operations are independent, set error="skip" or split them into separate blocks.
The <ignoreif> element inside operation skips the operation if the file already contains the specified text (or a regex match with regex="true"). It protects against double insertion when the same code has already been added by another module or manually by a developer.
How do you check that a modification was applied?
After each enabling, disabling, or deleting of a modification, click "Refresh": without it, storage/ remains in its old state. Then check in the following order:
- Open the log tab on the modifications page. The log is appended on every refresh, so look at the last block or first clear it with the clear log button.
- Find the line
MOD: <name>of your modification. Below it comeFILE:(orFILE: (sub modification)if the file has already been changed by a previous modification),CODE:orREGEX:with the search text, andLINE:with the number of the matching line. - The message
NOT FOUND - …means the line was not found. Comparesearchwith the file, keeping in mind that earlier modifications may have already changed it. - Open the copy of the file in
storage/ormodification/ catalog/ … …/admin/…and make sure the change is there.
If checking the log and file copies manually is inconvenient, you can use the free module OCT OCMOD Editor: OCMOD modification editor and checker for OpenCart 3.0. It applies modifications in memory in the same order and by the same rules as "Refresh". Before the refresh, the module shows for each operation whether the line was found and at which number. This makes silent failures described in this article immediately visible: an unfound search, a faulty regex that empties the file, or error="abort" that disables modifications later in the queue.
During development, it is convenient to place XML directly in system/ with the name *.ocmod.xml: such files are applied on "Refresh" without uploading an archive, but they also do not appear in the modifications list, so remember to remove them before handing over the work.
Versions 3.0 behave differently here. OpenCart 3.0.4.1: XML without a root <modification> or an operation without <search> or <add> aborts the refresh with a fatal error, and the store may stay in maintenance mode, because restoring the setting is at the end of the method. OpenCart 3.0.5.1: such places are skipped, and the refresh completes.
If the change is needed only in the output data of a controller, model, or template, consider events: they do not depend on the exact text of a line. See the articles changing a template via view/*/before and intercepting models via model/*/after. For a broader overview of OCMOD, see the blog: OCMOD in OpenCart: code changes without modifying system files.
Summary
- OCMOD 3.0 is applied with the "Refresh" button: copies of modified files go to
storage/, and the functionmodification/ modification()includes them thanks tosystem/.modification.xml codeis required and unique; reinstalling with the same code replaces the modification.- A regular
searchsearches within a single line, case-insensitively;positioncan only bereplace,before, orafter, and an unknown value works asreplace. - For multiple lines, use
regex="true"withpreg_replace()across the whole file. - Without
error="skip", an unfound operation cancels the remaining operations of thefileblock. - Read
ocmod.log: it shows exactly which operation did not find its line.
How OCMOD changed in 4.1 and how to find a modification that was not applied are covered in separate articles in the section Events and OCMOD. The locations of files that are commonly modified are described in the article OpenCart file structure.