Oct 082017
 

One thing to consider when you’re writing a Zen Cart plugin that includes an observer-class module is the point at which any monitored notification is going to be “fired”.  Auto-loading observers, previously discussed here, are great since using that method can reduce the number of files that a plugin needs in its distribution … but there’s a potential downside (isn’t there always?).

All auto-loading observers are loaded at breakpoint 175 during the Zen Cart autoload process.  That processing loads all files present in the /includes/classes/observers directory whose names match auto.*.php and, if the class’ name adheres to the auto-loading “rules”, creates an instance of the associated class.

If the notifications that your plugin is observing are issued prior to breakpoint 175, then auto-loading isn’t for you!

For example, let’s say that your plugin is monitoring NOTIFIER_CART_ADD_CART_START, issued by the shopping-cart class at the beginning of an add-to-cart action.  Those cart-manipulation actions occur during init_cart_handler.php‘s processing at breakpoint 140, so an auto-loading observer will never see that notification.  Your plugin will need to include an additional file (in /includes/auto_loaders)  to instruct Zen Cart the point at which your observer should be loaded (prior to 140!) and instantiated and its observer-class file’s name must not start with “auto.“.

In summary, while auto-loading observers can simplify a plugin’s distribution, plugin authors need to ensure that auto-loading is compatible with the plugin’s expected functionality.

Zen Cart v1.5.3: Auto-loaded Observers!

 php, plugins, v1.5.3, zen-cart  Comments Off on Zen Cart v1.5.3: Auto-loaded Observers!
Aug 312014
 

One of the features quietly introduced in Zen Cart v1.5.3 is auto-loaded observers.  If you’re developing a plugin that uses an observer-class, you normally have to provide two files in your plugin’s distribution to get that class loaded and instantiated:

  1. /includes/auto_loaders/config.your_plugin.php
  2. /includes/classes/observers/class.your_plugin.php

Starting with Zen Cart v1.5.3, built-in functionality will do the “heavy lifting” to get your class-file loaded and instantiated — so long as your class doesn’t have any special requirements on its load point (auto-loaded classes are loaded at point 175, after all other system dependencies are loaded).   Here are the requirements (as pulled from the file /includes/init_includes/init_observers.php):

  1. The file is in the /includes/classes/observers folder and named auto.your_plugin.php.  All files in this directory that start with auto. will be included (i.e. loaded).
  2. The file defines a class named zcObserver + the CamelCased filename, e.g. a file named auto.your_plugin.php will contain a class named zcObserverYourPlugin.  A myDEBUG*.log file will be generated if a properly-named file is loaded, but the class name doesn’t conform to these rules.

Here’s an example that displays a message in the header, but only on the index (main) page; the file is /includes/classes/observers/auto.test_observer.php:

<?php
// -----
// An example of an auto-loaded observer, based on a feature introduced in Zen Cart v1.5.3.
//
// Displays a header-message *only* on the index page.
//
class zcObserverTestObserver extends base {

  function __construct() {
    $this->attach($this, array('NOTIFY_HEADER_START_INDEX'));
    
  }

  function update (&$class, $eventID, $paramsArray = array()) {
    global $messageStack;
    $messageStack->add ('header', 'The auto-loaded observer is loaded!', 'success');
    
  }
}

Happy auto-loading!

Zen Cart Observers … They Work in the Admin, Too!

 hints, Uncategorized, v1.5.0, v1.5.1, zen-cart  Comments Off on Zen Cart Observers … They Work in the Admin, Too!
Sep 222013
 

The Zen Cart Observer Class is a great tool for plugin developers, allowing a plugin to watch for (“observe”) the occurrence of an event (a “notification”) and provide additional processing without making modifications to Zen Cart core files.  What hasn’t been stressed over the past years is that this notifier/observer interface also works in the Zen Cart admin panel!

It seems like every plugin (and its brother) need just a small update to /YOUR_ADMIN/orders.php, making the plugin installation (and Zen Cart upgrade) process just that more complex.  If plugin authors, myself included, would start adding a notifier at specific processing points in the popular (e.g. orders.php, customer.php) admin pages and providing observer code to insert their customizations, both the installation and upgrade processes would go much more smoothly.