# OCMOD syntax in OpenCart 3.0: search, add, regex and error handling

> How OpenCart 3.0 applies OCMOD: install.xml structure, line-by-line search and insert, regex, offset, index, error and ignoreif, and how to read ocmod.log.

Джерело: https://opencartcode.com/en/wiki/dev-events-ocmod/ocmod-syntax
OpenCart: 3.0
Оновлено: 2026-11-28

OCMOD syntax in [OpenCart](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#opencart) 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/modification/` and includes them in place of the originals. Below are the exact search and replace rules from the code `admin/controller/marketplace/modification.php`, 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 [controller](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#controller) `marketplace/modification`, when the administrator clicks "Refresh" on the [Extensions](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#extensions) → Modifications page. The process is as follows:

1. During the refresh, maintenance mode is enabled (`config_maintenance`), and the previous value is restored at the end.
2. The entire contents of the modifications directory (`DIR_MODIFICATION`, by default `storage/modification/`) are deleted.
3. The XML files are collected: first `system/modification.xml`, then all files in `system/*.ocmod.xml`, then the enabled entries of the `modification` table.
4. 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.
5. Only files whose contents have changed are written to `storage/modification/`, and the log is appended to `ocmod.log` in the logs directory.

How does the core pick up these copies? Through the same mechanism. The file `system/modification.xml` 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/library/template/twig.php`. The function `modification()` returns the path to the copy in `storage/modification/` if it exists, or the path to the original.

> The "Clear" button deletes the copies of modified core files that `system/modification.xml` creates. After that, no modification takes effect until you click "Refresh" again.

One more detail for templates: the modified Twig copy is read only when no ready-made code is passed to the renderer. If the [template](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#template) 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](https://opencartcode.com/en/wiki/dev-architecture/opencart-storage).

Extensions → Modifications: Refresh and Clear buttons, log tab · OpenCart 3.0.5.1

## 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](https://opencartcode.com/en/wiki/dev-extensions/ocmod-zip).

Example

```xml
<?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` [attribute](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#attribute):

- The path must begin with `catalog/`, `admin/`, or `system/`. Other prefixes are ignored without any message, so you cannot modify the root `index.php`, for example.
- Multiple paths are separated by the `|` character.
- The path is expanded via `glob()` with `GLOB_BRACE`: `*` and a list in curly braces work, for example `catalog/controller/{product,checkout}/*.php`. An asterisk in the theme path (as in the example) lets you target all installed themes that contain such a file.
- 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/controller/marketplace/modification.php` · OpenCart 3.0.5.1 · lines 295–304

```php
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 `search` will 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 for `position="replace"` is done via `str_replace`, which is case-sensitive. Therefore the case in `search` must match the file exactly. Otherwise the line will be found (`LINE:` will appear in the log), but the replacement will silently not happen.
- **The `search` text is trimmed of spaces** if the `trim` attribute is not set or is `true`. So indentation in the search line does not matter.
- **The `add` text is, on the contrary, not trimmed** unless you explicitly specify `trim="true"`. Indentation and line breaks from CDATA go into the file as they are.
- **Without `index`, all matches in the file are changed.** The `index` attribute 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.

> For `replace`, only the occurrence of the text from `search` changes, not the whole line. To rewrite the whole line, specify its entire meaningful content in `search`.

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

```xml
<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 `search` are a full PCRE expression with delimiters and modifiers, as for `preg_match()`. The core uses the delimiter `~` so that slashes in paths don't need escaping.
- Back-references to groups `add` and `$1` work in `$2`; the attributes `position`, `offset`, `index`, and `trim` are ignored.
- Both `search` and `add` are always trimmed of spaces at the edges.
- The `limit` attribute limits the number of replacements; without it, all matches are changed.
- An error in the expression is dangerous: `preg_replace()` then returns `null`, 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 to `storage/modification/`. Test the expression separately, for example in `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/controller/marketplace/modification.php` · OpenCart 3.0.5.1 · lines 379–399

```php
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/modification/` remains in its old state. Then check in the following order:

1. 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.
2. Find the line `MOD: <name>` of your modification. Below it come `FILE:` (or `FILE: (sub modification)` if the file has already been changed by a previous modification), `CODE:` or `REGEX:` with the search text, and `LINE:` with the number of the matching line.
3. The message `NOT FOUND - …` means the line was not found. Compare `search` with the file, keeping in mind that earlier modifications may have already changed it.
4. Open the copy of the file in `storage/modification/catalog/…` or `…/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](https://opencartcode.com/en/marketplace/product/oct-ocmod-editor) 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](https://opencartcode.com/en/wiki/dev-events-ocmod/event-view-opencart) and [intercepting models via model/*/after](https://opencartcode.com/en/wiki/dev-events-ocmod/opencart-event-model-after). For a broader overview of OCMOD, see the blog: [OCMOD in OpenCart: code changes without modifying system files](https://opencartcode.com/en/blog/ocmod-v-opencart-pravki-kodu-bez-zmini-sistemnix-failiv).

## Summary

- OCMOD 3.0 is applied with the "Refresh" button: copies of modified files go to `storage/modification/`, and the function `modification()` includes them thanks to `system/modification.xml`.
- `code` is required and unique; reinstalling with the same code replaces the modification.
- A regular `search` searches within a single line, case-insensitively; `position` can only be `replace`, `before`, or `after`, and an unknown value works as `replace`.
- For multiple lines, use `regex="true"` with `preg_replace()` across the whole file.
- Without `error="skip"`, an unfound operation cancels the remaining operations of the `file` block.
- 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](https://opencartcode.com/en/wiki/dev-events-ocmod). The locations of files that are commonly modified are described in the article [OpenCart file structure](https://opencartcode.com/en/wiki/dev-architecture/opencart-file-structure).

## FAQ

**In what order are several modifications of the same file applied?**

First system/modification.xml, then the system/*.ocmod.xml files, then enabled entries in the modification table in alphabetical order of their names (the model sorts by name). To change the order, rename the modification. Each subsequent modification searches for lines in the already changed text.

**Can OCMOD change a module file rather than a core file?**

Yes, if the file is in catalog/, admin/ or system/ and is loaded through mechanisms wrapped by modification(), such as a controller or model. The path is specified in path the same way as for core files.

**Why do changes remain on the site after a modification is disabled?**

Disabling only changes the status in the table, while copies in storage/modification are rebuilt only by the "Refresh" button. Also check the theme and browser cache if a template was changed.
