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
我已经搜索了很多关于opencart触发器但没有找到合适的例子。在opencart 2.0中,有一些触发器,开发人员可以在其中挂钩函数并执行类似wordpress操作和过滤器的操作。例如在
catalog/model/checkout/order.php
there is a trigger $this->event->trigger('post.order.history.add', $order_id)
有一个触发器$ this-> event-> trigger('post.order.history.add',$ order_id)
Can someone help me to hook my function on the above trigger?
有人可以帮助我在上面的触发器上挂钩我的功能吗?
2 个解决方案
#1
5
Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
重要说明:此答案适用于OC> 2.0.x.x和<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).
这里的问题是使用(和搜索)一个错误的单词 - 你应该搜索的正确的是事件,并从它派生事件监听器和触发器事件(不幸的是,当试图搜索那些和2.0的文档仍然缺失)。
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):
现在我相信整个背景更容易理解,特别是如果你对其他框架中的事件有一些了解(也许是jQuery?),这里只是一个如何处理事件的快速指南(在OC 2.0中):
-
first we need to register an event listener, like this:
首先我们需要注册一个事件监听器,如下所示:
$this->event->register('post.order.history.add', 'checkout/order/send_email');
$ 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);
$ this-> event-> trigger('pre.order.history.add',$ order_id);
and
和
$this->event->trigger('post.order.history.add', $order_id);
$ 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 trigger如果事件(由它的名称post.order.history.add标识)监听器被注册,它将在触发器上调用
For 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).
有关更多信息或自行解决,您可以查看system / engine / event.php(现在没有其他工作可做了)。
#2
5
Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
重要说明:此答案适用于OC> 2.0.x.x和<2.2.x.x.
The event system works like this:
事件系统的工作方式如下:
- OpenCart loads a list with all registered event handlers from the database. This happens at the end of the index.php file.
- OpenCart从数据库加载包含所有已注册事件处理程序的列表。这发生在index.php文件的末尾。
- Then the event handlers are registered in the $event object, which is an instance of the Event class (system/engine/event.php)
- 然后事件处理程序在$ event对象中注册,该对象是Event类的一个实例(system / engine / event.php)
- Then the $event->trigger() method is being called from different parts of the system. The trigger method accepts an event name as a parameter and all event handlers registered for this event name are being executed.
- 然后从系统的不同部分调用$ event-> trigger()方法。触发器方法接受事件名称作为参数,并且正在执行为此事件名称注册的所有事件处理程序。
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).
您可以使用$ event对象在运行时注册事件处理程序或触发事件,但仅在特殊情况下执行此操作。请记住,您很可能需要通过$ this-> event而不是$ event(取决于您需要它的位置)来访问$ event对象。
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:
通常,您只需使用扩展/事件模型在db表中注册事件处理程序一次。例如,您可以在管理控制器的install()方法中执行此操作。像这样的东西:
public function install() {
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
$this->model_extension_event->addEvent('mymodule', 'post.customer.add', 'module/mymodule/on_customer_add');
}
The event handlers are the third parameter of the addEvent() method and they are in the form of a standard route.
事件处理程序是addEvent()方法的第三个参数,它们采用标准路由的形式。
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.
您可以在此处找到有关事件系统的更多信息:http://isenselabs.com/posts/opencart2-event-system-tutorial。它是一个教程,解释了事件系统的工作原理,并提供了简单的示例,向您展示如何在扩展中使用它。
#1
5
Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
重要说明:此答案适用于OC> 2.0.x.x和<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).
这里的问题是使用(和搜索)一个错误的单词 - 你应该搜索的正确的是事件,并从它派生事件监听器和触发器事件(不幸的是,当试图搜索那些和2.0的文档仍然缺失)。
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):
现在我相信整个背景更容易理解,特别是如果你对其他框架中的事件有一些了解(也许是jQuery?),这里只是一个如何处理事件的快速指南(在OC 2.0中):
-
first we need to register an event listener, like this:
首先我们需要注册一个事件监听器,如下所示:
$this->event->register('post.order.history.add', 'checkout/order/send_email');
$ 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);
$ this-> event-> trigger('pre.order.history.add',$ order_id);
and
和
$this->event->trigger('post.order.history.add', $order_id);
$ 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 trigger如果事件(由它的名称post.order.history.add标识)监听器被注册,它将在触发器上调用
For 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).
有关更多信息或自行解决,您可以查看system / engine / event.php(现在没有其他工作可做了)。
#2
5
Important Note: this answer applies to OC >2.0.x.x and <2.2.x.x.
重要说明:此答案适用于OC> 2.0.x.x和<2.2.x.x.
The event system works like this:
事件系统的工作方式如下:
- OpenCart loads a list with all registered event handlers from the database. This happens at the end of the index.php file.
- OpenCart从数据库加载包含所有已注册事件处理程序的列表。这发生在index.php文件的末尾。
- Then the event handlers are registered in the $event object, which is an instance of the Event class (system/engine/event.php)
- 然后事件处理程序在$ event对象中注册,该对象是Event类的一个实例(system / engine / event.php)
- Then the $event->trigger() method is being called from different parts of the system. The trigger method accepts an event name as a parameter and all event handlers registered for this event name are being executed.
- 然后从系统的不同部分调用$ event-> trigger()方法。触发器方法接受事件名称作为参数,并且正在执行为此事件名称注册的所有事件处理程序。
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).
您可以使用$ event对象在运行时注册事件处理程序或触发事件,但仅在特殊情况下执行此操作。请记住,您很可能需要通过$ this-> event而不是$ event(取决于您需要它的位置)来访问$ event对象。
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:
通常,您只需使用扩展/事件模型在db表中注册事件处理程序一次。例如,您可以在管理控制器的install()方法中执行此操作。像这样的东西:
public function install() {
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
$this->model_extension_event->addEvent('mymodule', 'post.customer.add', 'module/mymodule/on_customer_add');
}
The event handlers are the third parameter of the addEvent() method and they are in the form of a standard route.
事件处理程序是addEvent()方法的第三个参数,它们采用标准路由的形式。
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.
您可以在此处找到有关事件系统的更多信息:http://isenselabs.com/posts/opencart2-event-system-tutorial。它是一个教程,解释了事件系统的工作原理,并提供了简单的示例,向您展示如何在扩展中使用它。