I have searched a lot about opencart triggers but didn't find a proper example. In opencart 2.0 there are triggers on which developer can hook function and perform something just like wordpress action and filters i guess. For example in
catalog/model/checkout/order.php
there is a trigger $this->event->trigger('post.order.history.add', $order_id)
Can someone help me to hook my function on the above trigger?
I think this question should be updated since the update to OpenCart v2.3.x OC events have changed dramatically.
Before they resembled the Wordpress approach. You would have a list of predefined events with before
and after
trigger.
Starting from version 2.3.x and then changing a little bit in version 3.x, OC events became more dynamic, where the triggers are dynamically defined based on the path structure of the files.
Of course, there are some disadvantages:
I would recommend using Events where possible, and fallback to OCmod where events have no access to.
When a controller (model, view, library or language) is loaded via the loader class like so:
$this->load->controller('common/header');
the method controller()
in file system/engine/loader.php
does the following
before
for the route common/header
$trigger = $route;
$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/before', array(&$route, &$data));
$action = new Action($route);
$output = $action->execute($this->registry, array(&$data));
after
for the route common/header
$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/after', array(&$route, &$data, &$output));
this is very much the same for model, language, library and config.
OpenCart matches the currently loading controller route (like the one we loaded above) common/header
(same goes for model, view, language, and config) with the registered events in the system.
You will add a trigger when you register your event.
For Controller
common/home
in catalog before it loads
The trigger will look like this:
catalog/controller/common/home/before
For Model
sale/order
for methodgetOrder
in catalogafter
it loads
The trigger will look like this:
catalog/model/sale/order/getOrder/after
Important, in model you will always set the trigger for the method, unlike the controller, where there is a default method
index
which is often droppedFor View
catalog/product_form.twig
in admin on after
The trigger will look like this:
admin/view/catalog/product_form/after
remember that when adding an event to a view after, you will be modifying the &$output veriable which by now is not a twig file, but a fully compiled HTML. So there are no twig tags in the code any longer.
the best way to modify your view if by using the PHP Simple HTML Dom Parser
There are three options to register an OpenCart Event.
system/config/catalog
edit $_['action_event']
I will show you how to add via the model since that is the recommended way.
in your file admin/controller/extension/module/my_module.php
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('my_module');
if($this->request->post['module_my_module_status']){
$this->model_setting_event->addEvent('my_module',
'admin/view/catalog/product_form/before',
'extension/module/my_module/view_catalog_product_form_before');
}
notice that I first remove all the events and then check if the status is true and add the events. this way I can be sure that no old or unexpected events get added.
public function uninstall(){
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('my_module');
}
It is good practice to remove all events after your module if it is being uninstalled.
Right now you know that there are Events which have triggers. But what do the triggers actually do?
They just load the controller of the event and pass the data by reference.
This is why:
before
event and $route and $output on after
eventHere is an example of a Event function in file admin/controller/extension/module/my_module.php
public function view_catalog_product_form_before(&$route, &$data){
if($data && isset($this->request->get['product_id'])){
$product_id = $this->request->get['product_id'];
$this->load->model('extension/module/my_module');
$product_info = $this->model_extension_module_my_module->getMoreProductInfo($product_id);
if($product_info){
//here I am modifying the $data and it will pass on to the view with this new data because it was passed to this function by referance.
$data['more_info'] = $product_info;
}
}
//there is no result returned in Event functions.
//if you do return a result, you will override the default result that can cause unexpected consequences. best to avoid it.
}
Enjoy! If you have any questions, send us a message at https://dreamvention.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With