{"id":3478,"date":"2024-08-29T10:38:59","date_gmt":"2024-08-29T10:38:59","guid":{"rendered":"https:\/\/www.rcvtechnologies.com\/blog\/?p=3478"},"modified":"2024-11-13T12:36:16","modified_gmt":"2024-11-13T12:36:16","slug":"opencart-event","status":"publish","type":"post","link":"https:\/\/www.rcvtechnologies.com\/blog\/opencart-event\/","title":{"rendered":"How to use Opencart Events"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In Opencart 4, the <\/span><b>Event System<\/b><span style=\"font-weight: 400;\"> is a powerful feature that allows developers to\u00a0 execute specific actions at predefined points in the code execution process. This mechanism is particularly beneficial for extending the platform&#8217;s functionality without altering its core files, thereby maintaining the integrity and upgradability of the Opencart system.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When a certain event occurs, the event system calls all registered handlers for that event. Each handler can have its own code to handle its event.<\/span><\/p>\n<h2>How Opencart\u00a0 Events Work<\/h2>\n<p><span style=\"font-weight: 400;\">When an event is triggered, the system automatically calls all registered handlers associated with that event. Each handler can contain custom code to perform the desired actions, such as sending emails, updating records, or integrating with third-party services.<\/span><\/p>\n<h2>Event Types<\/h2>\n<h3>1. Pre-Events:<\/h3>\n<h3><span style=\"font-weight: 400;\">Triggered before a specific action occurs. For example, a pre-event can be triggered before an order is saved, allowing developers to perform custom actions like validating order data.<\/span><\/h3>\n<h3>2. Post-Events:<\/h3>\n<h3><span style=\"font-weight: 400;\">Triggered after a specific action occurs. For example, a post-event can be triggered after a product is edited, allowing developers to perform custom actions like updating related data.<\/span><\/h3>\n<h2>Creating and Registering an Event Listener<\/h2>\n<p><span style=\"font-weight: 400;\">To use Opencart events, developers need to create an event listener, which is a function or method that responds to a specific event. An event completes the tasks that a controller, a model, a theme override, a language, and a config needs to be achieved in the back-end of the store. Upon startup, the Engine automatically registers triggers, and actions, and sorts orders in both the admin\/controller\/startup\/event.php and catalog\/controller\/startup\/event.php files.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Event listeners are registered using the addEvent method from the Event class. The method takes three parameters: the event name, the class\/method to be executed, and the priority of the listener.<\/span><\/p>\n<h4><strong>Key Parameters for `addEvent` Method<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>code<\/b><span style=\"font-weight: 400;\">: Unique identifier for the event hook.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>trigger<\/b><span style=\"font-weight: 400;\">: The specific event that triggers the handler.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>action<\/b><span style=\"font-weight: 400;\">: The method or class that will handle the event.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>description<\/b><span style=\"font-weight: 400;\">: A brief description of what the event does.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>status<\/b><span style=\"font-weight: 400;\">: Indicates whether the event is active (1 for enabled, 0 for disabled).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>sort_order<\/b><span style=\"font-weight: 400;\">: The order in which the event is executed.<\/span><\/li>\n<\/ul>\n<h2>Example: Registering an Event<\/h2>\n<h3><span style=\"font-weight: 400;\">Below is an example of how to register an event in Opencart:<\/span><\/h3>\n<p><strong>NOTE : This block of code is written on admin\/controller\/module\/file.<\/strong><\/p>\n<div class=\"dm-code-snippet dark default  dm-normal-version\" style=\"background-color:#000;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-php\"><\/p>\n<pre class=\"dm-pre-admin-side\">&lt;?php\r\nnamespace Opencart\\Admin\\Controller\\Extension\\Itmindslabmodule\\Module;\r\nclass Itmindslabmodule extends \\Opencart\\System\\Engine\\Controller{\r\n    public function index() {\r\n    \/\/ your code\r\n    }\r\n    public function install() {\r\n        \/\/ registering events to show menu\r\n        $this-&gt;__registerEvents();\r\n    }\r\n\r\n    protected function __registerEvents() {\r\n       $this-&gt;model_setting_event-&gt;addEvent([\r\n            'code'        =&gt; \"IMLShowMenu\",\r\n            'trigger'     =&gt; \"catalog\/view\/common\/header\/after\",\r\n            'action'      =&gt; \"extension\/itmindslab_module\/event\/itmindslab_module\",\r\n            'description' =&gt; \"Customer Account Menu\",\r\n            'status'      =&gt; 1,\r\n            'sort_order'  =&gt; 0,\r\n        ]);\r\n    }\r\n\r\n    public function uninstall(){\r\n        $this-&gt;__unregisterEvents();\r\n    }\r\n\r\n    protected function __unregisterEvents(){\r\n        $this-&gt;load-&gt;model('setting\/event');\r\n\t$this-&gt;model_setting_event-&gt;deleteEventByCode('IMLShowMenu');\r\n    }\r\n}<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p><span style=\"font-weight: 400;\">Events can be registered in an extension\u2019s install method or directly in the controller if needed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It\u2019s important to unregister events in the extension\u2019s uninstall method to avoid any unwanted behavior after removing the extension.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After registering the hook, we will see it in the Events list in the admin and can view it. It looks like this:<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-3480 size-full\" src=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost.png\" alt=\"\" width=\"1680\" height=\"889\" srcset=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost.png 1680w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost-300x159.png 300w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost-1024x542.png 1024w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost-768x406.png 768w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-020-Events-localhost-1536x813.png 1536w\" sizes=\"(max-width: 1680px) 100vw, 1680px\" \/><\/p>\n<h2><strong>Handling the Event in the Controller<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">In the controller file of your module, you would then add a method to handle the event:<\/span><\/p>\n<p>This code is written in catalog\/controller\/event\/file.<\/p>\n<div class=\"dm-code-snippet dark default  dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-php\"><\/p>\n<pre class=\"dm-pre-admin-side\">&lt;?php\r\n\r\nnamespace Opencart\\Catalog\\Controller\\Extension\\Itmindslabmodule\\Event;\r\n\r\nclass Itmindslabmodule extends \\Opencart\\System\\Engine\\Controller\r\n{\r\n    \r\n    \/**\r\n     * index\r\n     * @param  mixed $route\r\n     * @param  mixed $data\r\n     * @param  mixed $output\r\n     * @return void\r\n     *\/\r\n    public function index(&amp;$route = false, &amp;$data = array(), &amp;$output = array()): void {\r\n\r\n        $template_buffer = $this-&gt;getTemplateBuffer($route, $output);\r\n\r\n        $layout =   '&lt;li class=\"list-inline-item\"&gt;\r\n                        &lt;a href=\"#javascript\"&gt;\r\n                            &lt;i class=\"fa-solid fa-cog\"&gt;&lt;\/i&gt;\r\n                        &lt;\/a&gt; \r\n                        &lt;span class=\"d-none d-md-inline\"&gt;IML Settings&lt;\/span&gt;\r\n                    &lt;\/li&gt;';\r\n        $find = '&lt;div class=\"nav float-start\"&gt;';\r\n        $replace = $find . $layout;\r\n        $output = str_replace($find, $replace, $template_buffer);\r\n    }\r\n\r\n    \/**\r\n     * getTemplateBuffer\r\n     * @param  mixed $route\r\n     * @param  mixed $event_template_buffer\r\n     * @return string\r\n     *\/\r\n    protected function getTemplateBuffer($route, $event_template_buffer) {\r\n\r\n        \/\/ if there already is a modified template from view\/*\/before events use that one\r\n        if ($event_template_buffer) {\r\n            return $event_template_buffer;\r\n        }\r\n    }\r\n}<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">The Event <\/span><span style=\"font-weight: 400;\">looks like in the front end.<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-3481 size-full\" src=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost.png\" alt=\"\" width=\"1680\" height=\"889\" srcset=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost.png 1680w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost-300x159.png 300w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost-1024x542.png 1024w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost-768x406.png 768w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/08\/FireShot-Capture-023-Your-Store-localhost-1536x813.png 1536w\" sizes=\"(max-width: 1680px) 100vw, 1680px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><b>Advantages of Events in Opencart<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Flexibility:<\/b><span style=\"font-weight: 400;\"> Events allow for modifications and extensions without altering core files, thus simplifying development and maintenance.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Speed:<\/b><span style=\"font-weight: 400;\"> The system is efficient, executing event handlers only when needed.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Compatibility:<\/b><span style=\"font-weight: 400;\"> The event system is designed to be backward-compatible with previous <strong>Opencart<\/strong> versions.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ease of Use:<\/b><span style=\"font-weight: 400;\"> Even developers with minimal experience in extension development can use the event system to add new functionalities or modify existing ones.<\/span><\/li>\n<\/ul>\n<h3><b>Conclusion<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The event system in Opencart is a versatile tool for extending and modifying the platform&#8217;s behavior without directly interacting with its core files. By leveraging events, developers can create more maintainable, upgrade-friendly extensions that enhance the overall functionality of an Opencart store.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Opencart 4, the Event System is a powerful feature that allows developers to\u00a0 execute specific actions at predefined points in the code execution process. This mechanism is particularly beneficial for extending the platform&#8217;s functionality without altering its core files, thereby maintaining the integrity and upgradability of the Opencart system. When a certain event occurs, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3487,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[136],"tags":[138,140,139],"class_list":["post-3478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencart","tag-opencart-4","tag-php","tag-software-development"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=3478"}],"version-history":[{"count":16,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3478\/revisions"}],"predecessor-version":[{"id":3854,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3478\/revisions\/3854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/media\/3487"}],"wp:attachment":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=3478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=3478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=3478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}