Notice: Trying to get property of non-object in /var/www/html/yellopen/catalog/model/magikblog/article.php on line 307Notice: Trying to get property of non-object in /var/www/html/yellopen/catalog/model/magikblog/article.php on line 296 OpenCart 2.3.x Event System

OpenCart 2.3.x Event System

OpenCart 2.2+ introduced a new events system, in which hooks that can be called before and after events such as a controller method call, model method call or templates being loaded. They give the ability to manipulate the input method parameters and returned output.

OpenCart events system allows you to write code that will be executed when certain events occurring in OpenCart like customer registration, order processing, add products etc. or other key events that have the line with the call trigger, and all this without the need of changing core files.

Making a real use of events involves following steps:

Step 1 : Register Event

When your module is installed you will need to register all events that your script needs. You have to register event handlers in the install() method of module under admin/controller/extension/module/modulename.php as follows -

public function install() {
  $this->load->model('extension/event');
  $this->model_extension_event->addEvent('modulename',  'admin/model/catalog/product/addProduct/after', 'modulename/events/add_product');
}
In the above code modulename is the a unique code that is unique only to your extensions. You could use any name or your OpenCart username then the name of the extension.

After installing the module you can verify the event is registered properly or not from Admin > Extensions > Events 

Event List

Step 2 : Implement Event

Now lets implement the event handlers. The admin/model/catalog/product/addProduct/after event is emitted in the admin area, so its handler must be implemented in the admin controller file for our module. We would like our event handler method to do some action when new product is added from admin. Events with before method are emitted before the action is executed, and events with after method are executed after the action has been taken. 

 public function add_product($route,$product_id) {
		
	$this->load->model('sale/order');
	$this->load->model('catalog/product');
        
        //$product_id - you can get the product id that was recentlt added in this function	
		
   }

Step 3: Remove Event

When your module is uninstalled you will need to ensure that you remove all of the events that you registered in install method. You can remove the events using following code -

public function uninstall() {
	$this->load->model('extension/event');
	$this->model_extension_event->deleteEvent('modulename');
}

Understanding the events in OpenCart is beneficial in many ways. When possible you should always opt for the events instead of a modification system like vQmod and OCMOD or Override Engine. It is less likely that your extensions will collide with the other 3rd party extensions. Event system allows inter-module communication, so you can make several modules working together easily.

I hope this is enough to get you started with a OpenCart Event system. Please let us know if you have any questions or face an issue implementing this at - support@yellopen.com