Zen-cart development tutorial-notification/Observer Mode

Source: Internet
Author: User

Through the rewrite and automatic loading mechanisms, Zen-Cart enables secondary developers to conveniently add their own functions to the core code, however, developers cannot add their own code to different places in the Zen-Cart core code without disrupting the original code. therefore, Zen-Cart introduces the notification/observer mode (observer/notifier system, ONS), which provides developers with access to the core Zen-Cart code, instead of destroying the content of the original file.

Section 1st base class

To implement the notification/Observer mode, we first introduce an important class: base class (class. base. php ).

The base class contains code that implements the notification/Observer mode. Therefore, to make the notification/Observer mode take effect, all classes in Zen-Cart are defined as subclasses of the base class, open a class file, such as language. php. You can see the code shown in. if you want your class to implement the notification/observation mode, please derive your class from the base class.

 class language extends base {
….
Submitter

The purpose of ONS is that developers can write code to wait for a specific event to occur. Once an event occurs, they can execute their own code. How can these events be defined and when will they be triggered?

The event can be triggered using the code shown below.

 $this->notify('EVENT_NAME');

 

Section 3 observer

The event has been triggered, but what is the use of this event for developers? To use the events of the notifiers, we must write code to listen to these events. In general, we need to write the Observer into a class and put it in the directory"Includes/classes/observersNext, let's take a look at how to write an observer class that listens to events.

 

 <?php
class myObserver extends base {
function myObserver() {
$this->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
}
function update(&$callingClass, $notifier, $paramsArray) {
... do some stuff
}
}

 

 

From the code, we can see that a class named myObserver is defined, and the myObserver class and the event NOTIFIER_CART_ADD_CARD_END (shopping_cart.php) are bound in the constructor.

When an event is triggered, the base class checks whether an observer class is listening for this event. If yes, the base class executes a method in the Observer class, here, when the NOTIFIER_CART_ADD_CARD_END event is triggered, the update method will be executed.

Parameter description:

Attach method.

& $ Observer-reference of the observer class to generate a unique ID for the new observer

$ EventIDArray-array of events to be monitored

Update Method

& $ CallingClass-reference of the class that triggers the event. You can use the variables in the callback class of this parameter.

$ Notifier-name of the Updater that triggers the update notification, because multiple notifiers may trigger events.

$ ParamsArray-the passed parameter, which indicates that this parameter may be used to provide some parameters to the observer

 

Section 4th notifier class

ONS is designed with an object-oriented approach. The observer expects to bind himself to a class that can be notified in his own method, however, many programs in Zen-Cart are not actually included in the class, such as the code in the page. to enable event notification in a general program, Zen-Cart designs a notifier class as a global notifier. if you want to create a class to listen to the event triggered by the code in the program (such as the code in the page header file), add notifier to the myObserver class, as shown in the following code.

 

 class myObserver extends base {
function myObserver() {
global $zco_notifier;
$zco_notifier->attach($this, array('NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION'));
}

 

Section 5th contains an observer File

It is worth noting that"Includes/classes/observersdirectory"The files in the directory are not automatically loaded, so you needApplication_top.phpThe file can automatically load the myObserver class. In the directory"Uplodes/auto_loaders"Add a file"Config. freeProduct. php", The file content is shown in table 3, Example 4 code.

 

 $autoLoadConfig[10][] = array('autoType'=>'class',
'loadFile'=>'observers/class.freeProduct.php');
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
'className'=>'freeProduct',
'objectName'=>'freeProduct');

 

Section 6th notifiers/observer-mode programming example

Sometimes, when a customer buys a certain amount of goods, if the total amount purchased exceeds the specified quantity, we hope to give the customer a free small gift.

After analysis, you can know that after the customer adds the product to the shopping cart, it will check whether the purchased quantity exceeds the specified minimum consumption quantity, this gift should be automatically added to the customer's shopping cart. If the customer deletes the items in the shopping cart, it also needs to be checked. If the total consumption is smaller than the minimum consumption, the gift should be automatically deleted from the shopping cart. according to the traditional programming method, it is not difficult to implement this function, but it will damage the core code of Zen-Cart in many places. now, with ONS, we can use a very simple custom class to complete this function without damaging the core code of Zen-Cart.

 

Notification/Observer programming example

 <? Php
/**
* Observer class used to add a free product to the cart if the user has Ds more than $ x
*
*/
Class freeProduct extends base {
/**
* The threshold amount the customer needs to spend.
* Total number of customers needing to consume
* Note this is defined in the shops base currency, and so works with multi currency shops
*
* @ Var decimal
*/
Var $ freeAmount = 50;
/**
* The id of the free product.
* Gift ID
* Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer
* To be charged shipping on this, that you have it set correctly.
*
* @ Var integer
*/
Var $ freeProductID = 57;
/**
* Constructor method
* Constructor
* Attaches our class to the $ _ SESSION ['cart'] class and watches for 2 notifier events.
*/
Function freeProduct (){
$ This-> attach ($ this, array ('notifier _ CART_ADD_CART_END ', 'notifier _ CART_REMOVE_END '));
}
/**
* Update Method
*
* Called by observed class when any of our notifiable events occur
*
* @ Param object $ class
* @ Param string $ eventID
*/
Function update (& $ class, $ eventID ){
If ($ _ SESSION ['cart']-> show_total () >=$ this-> freeAmount &&! $ _ SESSION ['cart']-> in_cart ($ this-> freeProductID )){
$ _ SESSION ['cart']-> add_cart ($ this-> freeProductID );
}
If ($ _ SESSION ['cart']-> show_total () <$ this-> freeAmount & $ _ SESSION ['cart']-> in_cart ($ this-> freeProductID )){
$ _ SESSION ['cart']-> remove ($ this-> freeProductID );
}
If ($ _ SESSION ['cart']-> in_cart ($ this-> freeProductID )){
$ _ SESSION ['cart']-> contents [$ this-> freeProductID] ['qty '] = 1;
}
}
}
?>

 

Of course, it may be impossible for developers to fully consider the issue, and sometimes Event Notifications are not necessarily carried out in the Code where we want them. In this case, it is still possible to make a few modifications to the core code. However, by using the observer mode, you can avoid modifying its core code as much as possible.

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: info-contact@alibabacloud.com 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.