Zen-cart Development Tutorial-Notification/Observer mode

Source: Internet
Author: User
Tags php class unique id
Zen-cart Development Tutorial-Notification/Observer modeBy rewriting the mechanism and the automatic loading mechanism, Zen-cart makes it easy for two of developers to add their own functionality to the core code, but developers cannot add their own code to different parts of the Zen-cart core code without destroying the original code. As a result, Zen-cart introduced the notification/Observer pattern (Observer/notifier system, ONS), which gives developers the means to access Zen-cart core code without destroying the content of their original files. section 1th base class

To implement the notification/Observer pattern, first introduce an important class: The base Class (class.base.php).

The base class contains code to implement the notification/observer pattern, so in order for the notification/observer pattern to take effect, all classes in Zen-cart are defined as subclasses of the base class, and open a class file, such as language.php, that you can see as shown in the code. If you want your own class to be able to implement the notification/observe this pattern, derive your class from the base class.

Class Language extends Base {.... Section 2nd Notice

ONS's intention is that developers can write code to wait for a particular event to occur and execute their own code once the event occurs. So how are these events defined and when are they triggered?

The event can be triggered by the code shown below.

$this-> Notify (' Event_Name ');

Section 3rd Observer

Events have been triggered, but what's the use for developers? In order to take advantage of the notification event, we have to write code to listen for these events. In general, we need to write the observer as a class and put it under the directory "Includes/classes/observers". Next, 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_e ND ')); Function Update (& $callingClass, $notifier, $paramsArray) {... do some stuff}}

From the code, you can see that we have defined a class named Myobserver, and that the Myobserver class and event Notifier_cart_add_card_end (shopping_cart.php) are bound in the constructor.

When an event is triggered, the base class detects whether the Observer class is listening to the event, and if so, the base class executes a method in the Observer class, where the Update method is executed when the Notifier_cart_add_card_end event is triggered.

Parameter description:

Attach method.

& $observer-A reference to the Observer class that generates a unique ID for the new observer

$eventIDArray-array of events to listen for

Update method

& $callingClass – A reference to the class that triggers the event, through which you can access the variables in the class

$notifier – Triggers the update notification name because multiple notifications may be heard to trigger the event

$paramsArray – The parameter passed, which informs that this parameter may be used to provide some parameters to the Observer

section 4th notifier class

ONS is designed for object-oriented thinking, and observers expect to bind themselves to a class that can be notified in their own methods, whereas many of the programs in Zen-cart are not actually included in the class, such as code on the page. In order to enable the notification of events in a general program, Zen-cart designed a notifier class as a global notification. If you want to create a class to listen for events triggered by a program (such as code in a page header file), you should 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 the Observer class file

It is worth noting that the files in the "includes/classes/observersdirectory" directory do not automatically load, so you need to allow application_top.php files to automatically load the Myobserver class. In the Directory " Includes/auto_loaders "adds a file" config.freeProduct.php "to the file as shown in the table 3‑4 code.

$autoLoadConfig [Ten] = Array (' Autotype ' => ' class ', ' LoadFile ' => ' observers/class.freeproduct.php ') ; $autoLoadConfig [[]] = Array (' autotype ' => ' classinstantiate ', ' className ' => ' freeproduct ', ' objec ') Tname ' => ' freeproduct ');

section 6th Notification/Observer mode programming instance

Sometimes a requirement is that when a customer buys a certain amount of merchandise, if the total amount purchased exceeds the specified quantity, we would like to give the customer a free small giveaway.

After analyzing, you can know that when the customer adds the merchandise to the shopping cart, detects whether the purchased quantity exceeds the specified minimum consumption amount, you should automatically add the gift to the customer's shopping cart, and if the customer deletes the item in the shopping cart, it also needs to be tested if the total amount of consumption is less than the minimum consumption amount, You should automatically remove the gift from your shopping cart. In traditional programming, it is not difficult to implement this feature, but it will destroy Zen-cart's core code in multiple places. Now with ONS, we can do this with a very simple custom class and not break the core code of Zen-cart.

Speaker/Observer Programming instance

? PHP/* * Observer class used to add a free product to the cart if the user spends more than $x * */class Freeproduct EX Tends Base {/* * * * * * The threshold amount the customer needs to spend. * Total number of customers to consume * this is defined in the shops base Currency, with multi currency shops * @var decimal */var $freeAmount = 50; * * * The ID of the free product. * The ID of the giveaway * note. This must is a true free product. e.g. Price = 0 Also Make sure-if you don ' t want the customer * to be charged shipping in this, which you have it set C orrectly. * * @var integer/var $freeProductID = 57; * * * * * Constructor Method * Construction Methods * 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_en D ')); }/* * Update method * * Called by observed class "Any of our notifiable events occur * @param object $class * @pa Ram String $eventID */function Update (& $class, $eventID) {if ($_session [' cart ']-> show_total () >= $this-> Freeamount &&! $_session [' cart ']-> in_cart ($this-> Freeproductid)) {$_session [' cart ']-> add_cart ($this-> fr Eeproductid); } if ($_session [' cart]-> show_total () < $this-> freeamount && $_session [' cart ']-> In_ca RT ($this-> Freeproductid)) {$_session [' cart ']-> remove ($this-> freeproductid);} if ($_session [' C Art ']-> in_cart ($this-> Freeproductid)) {$_session [' cart ']-> contents[$this-> freeproductid][' Qty '] = 1; }}}?>

Of course, perhaps because developers can not be considered very well, sometimes in the code is not necessarily where we want to have an event notification. In this case, it is possible to make minor changes to the core code. However, by using the Observer pattern, 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.