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 → 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 controllerCode that receives a request and prepares data. More in the glossary → 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_encoderesponses (see the article on AJAX in 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 logThe error.log file, visible in "System → Maintenance → Error Logs." The first place to look when something breaks. More in the glossary →. How the Log class works and what ends up in the main log is covered in the article on the error log in OpenCart.
Example: quick dump of a variable to a separate file (OpenCart 4.1)
$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/), 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.
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
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 thesystem/class and look at theengine/ event.php $eventargument. 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. - 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/. The 3.0 core itself ships with a modification system/ 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:
- Check whether a copy of the file exists in
storage/ormodification/ catalog/ ... storage/. If it does, set the breakpoint there.modification/ admin/ ... - After editing the original, click "Refresh" 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 → → Modifications", otherwise the copy will stay outdated.
- 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.
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) and replay it with curl using the
XDEBUG_TRIGGER=1parameter in the URL. - Cron in 4.1 runs through the
cron/croncatalog route. The page "Extensions → Cron Jobs" shows the exact command with the address. To enable Xdebug, add theXDEBUG_TRIGGER=1parameter to this address, for exampleindex.php?route=cron/, or send thecron&XDEBUG_TRIGGER=1 XDEBUG_SESSIONcookie. How cron works in each version is described in the article on cron in OpenCart.
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
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 ofDB::querycalls from a single controller. - Redundant getProducts() calls in 3.0. The 3.0 storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → 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
$thisand->cache ->get() 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
$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/ 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.
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. 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/.
What else affects debugging results?
- OPcache. If
opcache.validate_timestamps=0is 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->cachesurvives code edits. If the result "doesn't change", delete your module'scache.*files in theDIR_CACHEdirectory (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->cachedata. - 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.
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. Outputjson_encodeinside an HTML comment or log$datafrom the controller. - Enable Xdebug with
start_with_request=triggerand correct path mapping. For callbacks and 4.1 cron, the trigger is theXDEBUG_TRIGGER=1parameter in the URL. - In 3.0, after "Refresh", copies from
storage/run; in 4.1, copies frommodification/ 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".