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?
Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
The problem here is a wrong word being used (and searched for) - the right one you should be searching for is event, and from it derived event listener and trigger event (unfortunately, hadn't luck when trying to search for those either and the documentation for 2.0 is still missing).
Now I believe the whole background is much more understandable, especially if you have some knowledge about events from other frameworks (maybe jQuery?) but here is just a quick guide how to work with events (in OC 2.0):
first we need to register an event listener, like this:
$this->event->register('post.order.history.add', 'checkout/order/send_email');
on certain places an event is triggered, e.g.
$this->event->trigger('pre.order.history.add', $order_id);
and
$this->event->trigger('post.order.history.add', $order_id);
if the event (identified by it's name
post.order.history.add
) listener was registered it will be invoked on triggerFor more information or to figure it out on your own you may have a look into
system/engine/event.php
(there is nothing more to work with right now).Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
The event system works like this:
You can use the $event object to register event handlers or trigger events at runtime, but do this in special cases only. Have in mind that you will most likely need to access the $event object through $this->event and not $event (depending on where you need it).
Most often, you will need to register your event handlers in the db table only once using the extension/event model . You can do this in your install() method of your admin controller, for example. Something like this:
The event handlers are the third parameter of the addEvent() method and they are in the form of a standard route.
You can find more about the event system here: http://isenselabs.com/posts/opencart2-event-system-tutorial. It is a tutorial explaining how the event system works and has simple examples which show you how to make use of it in your extensions.
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
andafter
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.
What are the benefits of using events.
Of course, there are some disadvantages:
How do OpenCart Events work
When a controller (model, view, library or language) is loaded via the loader class like so:
the method
controller()
in filesystem/engine/loader.php
does the followingbefore
for the routecommon/header
after
for the routecommon/header
this is very much the same for model, language, library and config.
What is an OpenCart event Trigger
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.
The trigger will look like this:
catalog/controller/common/home/before
The trigger will look like this:
catalog/model/sale/order/getOrder/after
The trigger will look like this:
admin/view/catalog/product_form/after
How to register an event
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
Where do I write OpenCart Event functions?
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 onafter
eventHere is an example of a Event function in file
admin/controller/extension/module/my_module.php
Enjoy! If you have any questions, send us a message at https://dreamvention.com