How to use the OpenCart 2.x event events System

Source: Internet
Author: User
Tags opencart

How to use the OpenCart 2.x event events System

OpenCart 2.x contains many new features, one of which is the event system, which is designed for developers. It allows you to avoid the risk of code conflicts by allowing you to modify your code without modifying the original system code (and , of course, not using Vqmod or the new version of Ocmod, which is a 2.x release. ), which specifies that certain actions are triggered when specific operations are performed. For example, you can use the event system to send notification messages to the background when the user orders or registers.

Principle of Use:

Using an event system requires two steps:

    1. Registers the event handler.

    2. Access the event handler.

Registering the event handler in the controller file is straightforward. You can use a separate file that contains all the methods as the event handler, or you can use a method in the controller. To register the event handler you need to use the Extension/event model (OpenCart 2.0.1+) or tool/event model (OpenCart The Extension/event model has two methods: Addevent ($code, $trigger, $action) register events and deleteevent ($code) Delete events. You can use addevent in the Intsall () method when you develop the plugin. Use Deleteevent in the method uninstall () that uninstalls the plug-in.

The $code parameter is used to assemble your event handler.

$trigger parameters are used to specify the action parameters at the time of the trigger . There are many predetermined OpenCart system trigger parameters: script-notifications).

The $action parameter is used to locate your event handler. It is usually a standard set of controller routes, such as: module/mymodule/on_user_created.


Environment: OpenCart 2.0.1+.

Suppose we are going to develop a module called "My module".

Background controller: admin/controller/module/mymodule.php .

Front desk file: catalog/controller/module/mymodule.php .

Requirements: When a user registers or deletes a store, send an email to the webmaster. The triggering parameters can be defined as and Post.customer.add.

First we can use the install () method in our module:

Public functionInstall() {$this-Load-Model(' Extension/event '); $this ->model_extension_event->< Span class= "PLN" >addevent ( ' mymodule ' , span class= "str" > ' ' ,  module/ Mymodule/on_store_delete '  $this ->model_extension_event->< Span class= "PLN" >addevent ( ' mymodule ' , span class= "str" > ' post.customer.add ' ,  Module/mymodule/on_ Customer_add ' }             /span>                

The process method for uninstalling the module is uninstall as follows:

Public functionInstall() {$this-Load-Model(' Extension/event '); $this ->model_extension_event->< Span class= "PLN" >addevent ( ' mymodule ' , span class= "str" > ' ' ,  module/ Mymodule/on_store_delete '  $this ->model_extension_event->< Span class= "PLN" >addevent ( ' mymodule ' , span class= "str" > ' post.customer.add ' ,  Module/mymodule/on_ Customer_add ' }             /span>                

Next we access the event handler. ' ' handles background events, so his processor must be plugged into the controller file in admin/. When the store is deleted, we need a processor method to send notifications to the backend administrator. to the pre. The beginning of the event indicates that the controller method is triggered before execution to post. The beginning of the event indicates that the Controller method is fired after execution. At the same time, we also want to include the name of the store address in our information, if we finish the operation of deleting the store, then we will not be able to get the domain name of the deleted store.

Event handler:

Public functionOn_store_delete($store _id) {$this-Load-Model(' Setting/store ');$store _info=$this-Model_setting_store-GetStore($store _id $admin _mail = $this -> config->get ( Config_email '  Mail ( $admin _mail, span class= "str" > "A Store has been deleted" ,  "the store"  . $store _info[ ' URL ' ] . "was deleted." }             /span>                

The post.customer.add needs to write to the event handler in the foreground controller catalog. Notifies the background administrator when a new user is registered. A similar approach is as follows:

Public functionOn_customer_add($customer _id) {$this-Load-Model(' Account/customer ');$customer _info=$this-Model_account_customer-getcustomer ( $customer _id= $this ->config ->get ( ' Config_email '  Mail ( $admin _mail, span class= "str" > "new Customer" ,  "A New customer has just Registered with the following e-mail: " .[ ' email ' );                /span>                

Note: We use the mail () function to send the message to the real situation, we may want to use the OpenCart mail class to send e-mails.

The final code is as follows:

<?PhpClass Controllermodulemymodule Extends Controller { Public functionInstall() {$this-Load-Model(' Extension/event ');$this-Model_extension_event-Addevent(' MyModule ', ' ', ' Module/mymodule/on_store_delete ');$this-Model_extension_event-Addevent(' MyModule ', ' Post.customer.add ', ' Module/mymodule/on_customer_add '); } Public functionUninstall() {$this-Load-Model(' Extension/event ');$this-Model_extension_event-Deleteevent(' MyModule '); } Public functionOn_store_delete($store _id) {$this-Load-Model(' Setting/store ');$store _info=$this-Model_setting_store-GetStore($store _id);$admin _mail= $this ->config- >get ( ' config_email '  ( $admin _mail,  "A Store has been deleted ", " the store "  $store _info[ ' URL ' ] Span class= "pun".   "was deleted."  }}       /span>                


<?PhpClass Controllermodulemymodule Extends Controller { Public functionOn_customer_add($customer _id) {$this-Load-Model(' Account/customer ');$customer _info=$this-Model_account_customer-GetCustomer($customer _id); $admin _mail = $this,config,get(' config_email '); Mail( $admin _mail, "New Customer", "A New customer have just registered with the following e-m AIL: " .  $customer _info[' email ']);  }}< /c11> 

In addition to the standard usage described above, event systems can also be used to create cross-module interfaces. Using the Event object ($this->event), you can trigger any event anywhere. You can use it to trigger your custom events. Imagine that you are developing a module for user reviews. You can trigger an event when a customer sends a comment, which allows other module developers to create a custom processing method for your event processing period without having to use vqmod or Ocmod to modify the code. It ensures that the opencart becomes more stable.

The Ps:event class is defined in the system/engine/event.php file.

The Opencart 2.x comes with a list of event handlers.

How to use the OpenCart 2.x event events System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.