# OpenCart Debugging: Xdebug, Logging and Profiling a Module on 3.0 and 4.1

> How to debug an OpenCart 3.0 and 4.1 module: diagnostic logs, dumping Twig variables, Xdebug with breakpoints in controllers and events, and profiling slow pages and SQL.

Джерело: https://opencartcode.com/en/wiki/dev-practice/debug-opencart
OpenCart: 3.0, 4.1
Оновлено: 2026-11-28

[OpenCart](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#opencart) debugging relies on three tools: a diagnostic log to see data without breaking the response, Xdebug to step through code line by line, and a profiler to find where a page loses time. Neither 3.0 nor 4.1 has a built-in debug panel. There are, however, some quirks you need to know so that breakpoints fire and measurements are accurate: OCMOD file copies, the template cache, AJAX requests, and cron. Below is a practical set of tools for module developers.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling work differently.

## Why not var_dump, and what to use instead?

`var_dump()` or `print_r()` in the middle of a [controller](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#controller) only works on regular pages. In OpenCart, a lot of code runs where output breaks the result:

- AJAX checkout steps, cart, filters, and any `json_encode` responses (see the article on [AJAX in OpenCart](https://opencartcode.com/en/wiki/dev-mvc/ajax-opencart)): any text before the JSON makes the response invalid, and the page simply "does nothing";
- event handlers that fire before rendering or in the middle of another controller;
- payment gateway callbacks and cron, where there is no browser at all;
- redirects: any output before `header()` cancels the redirect.

That's why the basic tool is a separate module log, enabled in the settings and written to `DIR_LOGS`. In 4.1, such a file is immediately visible in its own tab in the admin [error log](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#error-log). How the `Log` class works and what ends up in the main log is covered in the article on [the error log in OpenCart](https://opencartcode.com/en/wiki/dev-practice/error-log-opencart).

Example: quick dump of a variable to a separate file (OpenCart 4.1)

```php
$debug = new \Opencart\System\Library\Log('debug.log');
$debug->write([
    'route' => $this->request->get['route'] ?? '',
    'post'  => $this->request->post,
    'cart'  => $this->cart->getProducts()
]);
```

The `write()` method passes values through `print_r(..., true)`, so arrays are written in a readable form. In 3.0, the same line is `new Log('debug.log')`. Such temporary code must not make it into a module release: search for it by file name before packaging.

### Dumping in a Twig template

Neither 3.0 nor 4.1 attaches `DebugExtension` to the template class (`system/library/template/twig.php`), so the `dump` function is not available in templates. Calling `{{ dump() }}` causes a render error, and the page breaks. To see what the controller passed to `$data`, output `{{ products|json_encode }}` inside an HTML comment or write `$data` to the log from the controller.

In both versions, Twig works with `auto_reload`, so a changed template recompiles automatically. You don't necessarily have to clear the theme cache after every edit, unless an OCMOD modification overrides the template. How a template receives variables is described in the article on [Twig in OpenCart](https://opencartcode.com/en/wiki/dev-mvc/opencart-twig-templates).

## How do you configure Xdebug for OpenCart?

Xdebug 3 is configured in php.ini (or in `conf.d` for PHP-FPM) with a few directives. For a local server or Docker, this is enough:

Example: minimal Xdebug 3 settings for step debugging

```bash
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
```

The `trigger` mode means Xdebug connects only to requests with a trigger, not to every request. This matters for a store: one OpenCart page pulls in dozens of requests for images, CSS, and AJAX, and with `yes` the IDE would keep stopping in the wrong places. The trigger in the browser is set by the Xdebug helper extension (cookie `XDEBUG_SESSION`). For a one-off request, you can add `XDEBUG_TRIGGER=1` to the URL. For Docker, set `client_host` to the host address, and in your IDE configure path mapping between the directory inside the container and the local project.

### Where to set breakpoints

- **A module controller method** is the simplest. If it doesn't stop, check the route: in 3.0 the method is separated by a slash, in 4.1 by a dot.
- **An event handler** fires only on its own trigger. To see which events are actually fired, set a breakpoint in the `trigger()` method of the `system/engine/event.php` class and look at the `$event` argument. This quickly shows why your trigger "doesn't fire": it simply doesn't match what the core generates. Trigger separators are described in the article on the [event system](https://opencartcode.com/en/wiki/dev-events-ocmod/events-opencart).
- **A model**: set it inside the method itself, not where the method is called. The `$this->model_*` call goes through the core's proxy, and the "step into" action first lands in proxy and event code.

### Breakpoint not firing: OCMOD file copies

This is the main debugging trap in OpenCart 3.0. After clicking "Refresh" on the modifications page, the core loads controllers, models, libraries, and templates not from the original files but from modified copies in `storage/modification/`. The 3.0 core itself ships with a modification `system/modification.xml` that inserts a call to `modification()` into the loader. If the file where you set a breakpoint is changed by any modification, the copy runs, and the IDE on the original will not stop. Editing the original also changes nothing until the next "Refresh".

What to do:

1. Check whether a copy of the file exists in `storage/modification/catalog/...` or `storage/modification/admin/...`. If it does, set the breakpoint there.
2. After editing the original, click "Refresh" in "[Extensions](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#extensions) → Modifications", otherwise the copy will stay outdated.
3. If you're unsure which file is actually running, temporarily write `__FILE__` to the log from that spot.

In 4.1, OCMOD places modified copies in the `extension/ocmod/` directory. The verification principle is the same: look for the copy and confirm via `__FILE__` that it is the one running. How OCMOD generates these files is described in the article on [diagnosing OCMOD](https://opencartcode.com/en/wiki/dev-events-ocmod/ocmod-not-working).

### AJAX, callbacks, and cron

- **AJAX** from the same tab automatically gets the trigger cookie. Checkout steps and admin buttons are debugged the same way as regular pages.
- **A payment gateway callback** arrives without your cookie. The easiest approach is to write the raw request to the log (see the article on [diagnosing callbacks](https://opencartcode.com/en/wiki/dev-flows/opencart-callback-not-updating-order-status)) and replay it with curl using the `XDEBUG_TRIGGER=1` parameter in the URL.
- **Cron in 4.1** runs through the `cron/cron` catalog route. The page "Extensions → Cron Jobs" shows the exact command with the address. To enable Xdebug, add the `XDEBUG_TRIGGER=1` parameter to this address, for example `index.php?route=cron/cron&XDEBUG_TRIGGER=1`, or send the `XDEBUG_SESSION` cookie. How cron works in each version is described in the article on [cron in OpenCart](https://opencartcode.com/en/wiki/dev-extensions/cron-opencart).

> Do not enable Xdebug on a production server, even with a trigger. The extension slows down every PHP request, and an open debug port is a way to execute code. Debug on a copy of the store with a copy of the database.

## How do you find where a page loses time?

For performance measurement, Xdebug has a separate profiler mode. For each request with a trigger, it writes a file `cachegrind.out.*` with the time and call count of every function.

Example: Xdebug 3 profiler with a trigger

```bash
xdebug.mode=profile
xdebug.start_with_request=trigger
xdebug.output_dir=/tmp/xdebug
```

Open the file in QCacheGrind/KCacheGrind, Webgrind, or your IDE's profiler. Sort by "inclusive time" and work from the top down. The profiler itself slows the request down many times over, so look at proportions rather than absolute milliseconds.

### What usually turns up in OpenCart modules

- **Model calls inside loops.** `getProduct()` for each product in a list, `getProductAttributes()` in a category loop, a custom query for each row. In the profiler, this shows up as hundreds of `DB::query` calls from a single controller.
- **Redundant getProducts() calls in 3.0.** The 3.0 [storefront](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#storefront) model first selects IDs and then makes a separate `getProduct()` for each one. A module that shows several product blocks on a page multiplies these requests.
- **Image resizing.** The first display of a page with new thumbnail sizes generates files in the image cache. Measure the second request, not the first, or the result will be overstated.
- **External HTTP requests during rendering**: exchange rates, shipping costs, module license checks. They appear as a long `curl_exec`.
- **Missing cache.** A module recalculates on every request what changes once a day (category tree, counters). Using `$this->cache->get()` and `set()` with a module key helps here.

### A simple measurement without a profiler

On a server without Xdebug, you can quickly measure a suspicious section through `microtime(true)` and the log:

Example: timing a section of code

```php
$start = microtime(true);

$results = $this->model_extension_mymodule_module_mymodule->getBlocks($setting);

$this->log->write(sprintf('mymodule getBlocks: %.1f ms, %d rows', (microtime(true) - $start) * 1000, count($results)));
```

For SQL, it's more convenient to enable the MySQL slow query log (`slow_query_log` with `long_query_time` around 0.5–1 s) on a copy of the site and look at `EXPLAIN` for the queries it finds. The core has no per-page query counter in either 3.0 or 4.1. The simplest option is to temporarily add a counter to the `query()` method of the `system/library/db.php` class on a development copy and write it to the log at the end of the request. How to read `EXPLAIN` and which indexes to add is covered in the article on [slow queries in OpenCart](https://opencartcode.com/en/wiki/dev-database/slow-queries-opencart).

For OpenCart 3.0, a free module gives this picture without Xdebug and without temporary edits in `db.php`: [OCT Performance Profiler: performance profiler for OpenCart 3](https://opencartcode.com/en/marketplace/product/oct-performance-profiler). It measures the page from the inside and shows how much time goes to the database versus code, as well as which module made how many queries and from which line. The results panel is visible only to the browser for which the profiler is enabled. The "Benchmark" tab shows how many milliseconds and queries each installed module adds. The module works without OCMOD and without changes to core files, so it creates no copies in `storage/modification/`.

## What else affects debugging results?

- **OPcache.** If `opcache.validate_timestamps=0` is enabled on the server, a changed PHP file won't be picked up until PHP-FPM restarts. On a development copy, keep file timestamp checking enabled.
- **OpenCart cache.** Data that a module stores in `$this->cache` survives code edits. If the result "doesn't change", delete your module's `cache.*` files in the `DIR_CACHE` directory (`storage/cache`). The button in the developer settings (the gear icon on the admin control panel) clears only the theme and SASS cache, not `$this->cache` data.
- **Error display.** On a development copy, turn on Display Errors: Notices show up right where they occur. If AJAX responses break as a result, that's a useful signal too.
- **Matching versions.** The PHP, MySQL, and OpenCart versions on the copy should match production, otherwise measurements and errors will differ. What to do when an error is already taking the site down is described in the article on [the white screen and the 500 error](https://opencartcode.com/en/wiki/dev-practice/opencart-white-screen).

## Summary

- Instead of `var_dump`, write data to a separate module log. Output breaks AJAX, redirects, and callbacks.
- The `{{ dump() }}` function doesn't exist in Twig in either 3.0 or 4.1. Output `json_encode` inside an HTML comment or log `$data` from the controller.
- Enable Xdebug with `start_with_request=trigger` and correct path mapping. For callbacks and 4.1 cron, the trigger is the `XDEBUG_TRIGGER=1` parameter in the URL.
- In 3.0, after "Refresh", copies from `storage/modification/` run; in 4.1, copies from `extension/ocmod/` run. Set breakpoints where the code actually executes.
- The Xdebug profiler shows models in loops, resizing, external requests, and missing cache. Measure the second request, not the first.
- Debug only on a copy of the site with the same software versions.

Other materials on debugging, performance, and module code quality are collected in the section ["Debugging, Performance, and Code Quality"](https://opencartcode.com/en/wiki/dev-practice).

## FAQ

**Can you debug the OpenCart admin the same way as the storefront?**

Yes, the Xdebug trigger works the same for admin/index.php. Just keep in mind that when editing orders, OpenCart 3.0 admin makes separate requests to the storefront API, so the breakpoint must be set in the controllers under catalog/controller/api.

**Why does the IDE stop at a different line than the one where the breakpoint is set?**

Most often a different copy of the file is being executed (an OCMOD modification) or the paths in the IDE are mapped incorrectly. Check __FILE__ in the log and your path mapping settings.

**How long is normal for an OpenCart page without cache?**

It depends on the server, the number of modules, and the products on the page. As a rough guide, generating a category page in a few hundred milliseconds is acceptable, while seconds almost always point to a specific problem that is visible in the profiler.

**Can you leave a debug log enabled in a module release?**

Yes, if it is disabled by default, can be turned on in the module settings, and does not write passwords, keys or customers' personal data.
