docs/developer-guide/events.md
OpenCart v4.x uses an event system to allow developers to extend and modify core functionality without altering the core codebase. The event system enables you to hook into specific points in the application lifecycle, execute custom code, and modify data or behavior. This guide explains how to work with events in OpenCart, including registering, triggering, and handling events, with practical examples.
The event system in OpenCart is based on a publisher-subscriber model. Events are triggered at specific points in the application, and developers can register listeners (handlers) to execute custom code when these events occur. This makes OpenCart highly extensible, allowing modifications like adding custom logic, altering data, or integrating third-party services.
catalog/controller/product/category/before).Events are typically identified by a string key in the format namespace/action/stage.
catalog, admin, extension).controller/checkout/cart.add).before or after).Example: catalog/controller/product/product/before is triggered before rendering the product view page in the catalog (front-end). Note that when referring to index method you have to let the action path without specifying the method (correct: product/product, wrong: product/product.index).
Here are some commonly used events you might want to hook into:
catalog/view/product/product/before: Before rendering a product page.catalog/view/common/content_top/after: After rendering content_top.catalog/controller/checkout/cart.add/after: After adding a product to the cart.catalog/model/checkout/order.editOrder/before: Before editing an order.catalog/controller/checkout/confirm/after: After confirming a checkout.Event listeners are registered in OpenCart through the event registry. You typically define listeners in your module or extension's code, either in a custom module or by modifying an existing extension.
Suppose you want to execute custom code before a product is added to the cart in the front-end (catalog/controller/checkout/cart.add/before).
Step 1: Create the Listener
Create a PHP class for your extension. For example, place the following code in extension/test_module/catalog/controller/events.php:
<?php
namespace Opencart\Catalog\Controller\Extension\TestModule;
class Events extends \Opencart\System\Engine\Controller {
public function onCartAddBefore(&$route, &$data, &$output = null) {
// Process your customizations here
$this->log->write('onCartAddBefore() has been successfully triggered!');
}
}
Step 2: Register the Event
Events are typically registered in the install() method of your extension. For programmatic registration, use the startup/event model.
Add the following code to your module’s controller:
<?php
namespace Opencart\Admin\Controller\Extension\TestModule\Module;
class TestModule extends \Opencart\System\Engine\Controller {
public function install(): void {
// Load the event model
$this->load->model('setting/event');
// Register the event
$this->model_setting_event->addEvent([
'description' => 'Test module - Event before cart add',
'code' => 'test_module_cart_add_before', // Event code (unique identifier)
'trigger' => 'catalog/controller/checkout/cart.add/before', // Event trigger
'action' => 'extension/test_module/events.onCartAddBefore', // Listener method
'status' => 1,
'sort_order' => 1
]);
}
public function uninstall(): void {
// Remove the event on uninstall
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('test_module_cart_add_before');
}
}
Step 3: Test the Event
Extensions > Installer).Extensions > Extensions (if config code "module_[module_name]_status" is not set then the corresponding event won't be triggered).System > Maintenance > Error logs) to verify that your custom log message appears.You can also trigger your own custom events. This is useful for custom modules or extensions that need to notify other parts of the system.
Suppose you want to trigger a custom event called extension/test_module/custom_action/after.
In your controller or model, use the trigger() method from the event registry. For example, in /extension/test_module/catalog/controller/custom_action.php:
// Data to pass to event
$data = ['message' => 'Custom action executed'];
// Trigger a custom event
$this->event->trigger('extension/test_module/custom_action/after', $data);
namespace/action/stage convention for clarity.uninstall() method to avoid orphaned entries.System > Settings > Server > Error Logging) to debug issues.Extensions > Events.