Page 1 of 4 123 ... LastLast
Results 1 to 10 of 33
  1. #1
    Join Date
    Nov 2004
    Location
    Estonia
    Posts
    40
    Plugin Contributions
    0

    Default Use Of Notifiers - How To

    I have read the wiki tutorial and previous v1.3 anouncment posts in the forum and I understand the concept of notifieriers, but cannot get it working...

    I presumed, that when I save a new class which has
    Code:
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
    or similar in the class consturctor, then the observer class should implement the code and wake it up, when notified, but nothing... [Tried also with the original code in the wiki]

    As far as I understand, the application_top does not read in the classes that are saved under includes/classes/observers catalog, so the class and its constructor that should attach notifier to praticular event is never called and observers array stays empty... Should aplication_top read in all observers that are saved into this folder or should I explicitly say somewhere, that these classes should be included, so the class constructors are waked and observers are added to observers array?

    Debuging the code with breakpoint at class.base notify function indicates indeed that $this->observers array is empty in spite of the fact that I copied the observer code from the wiki API...

  2. #2
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,876
    Blog Entries
    2
    Plugin Contributions
    2

    Default

    Hi,

    The includes/classes/observers directory is not an autoload directory, so you will need to arrange for application_top.php to autlload the class.

    lets assume you are using the freeProduct class example, and you have saved this in includes/classes/observers/class.freeProduct.php

    You now need to arrange for this class to be loaded and instantiated.

    To do this tou need to use tha application_top.php autoload system.

    In includes/autoloaders xreate a file called config.freeProduct.php containing
    Code:
    <?php
     $autoLoadConfig[90][] = array('autoType'=>'class',
                    'loadFile'=>'observers/class.freeProduct.php');
     $autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
                    'className'=>'freeProduct',
                    'objectName'=>'freeProduct');
    ?>
    Note: I chose 90 as the offset as the observer needs to attach to the $SESSION['cart'] class. This is instantiated at offset 80.

    The free product class should now work as advertised :)

  3. #3
    Join Date
    Nov 2004
    Location
    Estonia
    Posts
    40
    Plugin Contributions
    0

    Default

    Thank you&#33; :)

    I copied your reply into Wiki as well, see at between "Observe and Prosper" and "Real World examples" chapters.

  4. #4
    Join Date
    Nov 2004
    Location
    Estonia
    Posts
    40
    Plugin Contributions
    0

    Default

    I am still learning it...

    I got it working with the example above! However, when I just changed the notifier into NOTIFY_HEADER_START_CHECKOUT_CONFIRMATION or NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION then the script cannot find the observing script anymore...

    Debug'ign the code shows that the $this->observers array is empty, when the notifier returns from the checkout_process header, but the same array is filled when the notifier returns from chart (during the same debuging process ).

    I presume that $_SESSION['cart'] is not the right place to attach the notifier in the case when I would like to be notified by checkout_confirmation header (I tried to play around with the autoload position), but what is then a right place?

    Are there some tricks, how to load notifiers from different parts of the script?

  5. #5
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,876
    Blog Entries
    2
    Plugin Contributions
    2

    Default

    I copied your reply into Wiki as well, see at between "Observe and Prosper" and "Real World examples" chapters.
    ::tup

    On your other point.

    the observer/notifier system is really written for a completely OOPs based application, as the observer expects to attach to a class that has notifiers within its methods. However a lot of the code within ZC is still procedural in nature and not contained withn a class.

    To work around this, we added the &#39;stub&#39; notifier class. So if you want ot create an observer for a notifier that lies within procedural code you should do something like
    Code:
    &#036;zco_notifier-&#62;attach&#40;&#036;this, array&#40;&#39;NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION&#39;&#41;&#41;;
    I seemed to have missed this from the docs

  6. #6
    Join Date
    Nov 2004
    Location
    Estonia
    Posts
    40
    Plugin Contributions
    0

    Default

    Thanks!

    I just discovered it out just few moments ago and -- "boom" -- found also your reply in the forums :)

    The point, where I got stucked yesterday, was really silly: I actually tried to attach the notifier into $zco_notifier notifier class, but just forgot to add:

    Code:
    global $zco_notifier;
    before the $zco_notifier->attach statement in my observer class...

  7. #7
    Join Date
    Mar 2005
    Location
    London, Ontario
    Posts
    48
    Plugin Contributions
    0

    Default

    So quick question then. I&#39;ve got the notifier working fine. Trying to write a plugin that will sync newsletter subscriptions with PHPlist. However what means are there for passing parameters? I need to find out whether the person signed up to the newsletter or not.
    Jeremy Richardson
    www.justjerm.com

  8. #8
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,876
    Blog Entries
    2
    Plugin Contributions
    2

    Default

    Hi,

    There is no parameter passing at all.

    variables are available in a number of ways.

    If you are writing a notifier that attaches to a class, then the class is passed to the observer as $class, you can therefore query class variables as $class->class-variable.(note the above does not work for session based classes, like the cart, however you can reference these as normal as $_SESSION['cart'] etc.

    You can accesss procedural code variables using the superglobal $GLOBALS array, and of course all normal superglobals e.g. $_SESSION, $_POST, $_GET are available as normal.

    In your example you can access the $_SESSION['customer_id'] directly and then do a quick sql query to see if that customer is signed up to newsletters.

    HTH

  9. #9
    Join Date
    Mar 2005
    Location
    London, Ontario
    Posts
    48
    Plugin Contributions
    0

    Default

    Ok, I'm beggining to get the picture here.

    Next challenge. I've written a class to modify the description before it is displayed. I've used the NOTIFY_MAIN_TEMPLATE_VARS_EXTRA_PRODUCT_INFO notifier to run my code after the database has been queried and before it is displayed.

    I can get the description by using the GET variable products_id. However then how would I modify the variable products_description?

    What I'm trying to do is parse through the description text looking for keywords I have in an array (from a glossary table). I want to make any matching terms into hyperlinks.

    Related to this, I'm a little sketchy on how to tell the notifier is in a class or not. I'm assuming the class declaration should be in the same file as the notifier? ie NOTIFY_MAIN_TEMPLATE_VARS_EXTRA_PRODUCT_INFO is not in a class.

    thanks, I'm liking this notifier system more and more! Any idea what phase will see all notifiers contained in classes?
    Jeremy Richardson
    www.justjerm.com

  10. #10
    Join Date
    Sep 2004
    Posts
    14
    Plugin Contributions
    0

    Default Re: Use Of Notifiers - How To

    I am trying to write the observer class attached to events during the checkout process. I need the session class with several variables storing values for session.
    But I can not properly initialize them and get them reinitialized several times during the checkout process. A a result I loose the values assigned to these variables on previous entry to the class. It looks like I do not understand how does the ONS work.

    For test purposes I wrote a stupid testclass attached to the same events. But it does not work at all. I would appreciate if someone could advice what is wrong.

    The file config.testclass.php in the includes/auto_loaders directory contains:
    Code:
    <?php
    /*
     * instantiate class.testclass at 180 - when
     * everything is initialized
     * in <root>/includes/auto_loaders
     */
    
    $autoLoadConfig[180][]= array('autoType' => 'class',
    				'loadFile' => 'observers/class.testclass.php');
    
    $autoLoadConfig[180][]= array('autoType' => 'classInstantiate',
    				'className' => 'testclass',
    				'objectName' => 'testClassObject',
            'checkInstantiated' => true,
            'classSession' => true );
    
    ?>
    and class.testclass.php in includes/classes/observers looks like this:
    Code:
    <?php
    
    class testclass extends base {
      var $initialized= false;
      var $field;
      /*constructor*/
      function testclass() {
        global $zco_notifier;
    
        $this->test_log($this->initialized ."\t=initialized at start constructor");
        $this->test_log($this->field ."\t=field at start constructor");
        $zco_notifier -> attach($this,
                                array('NOTIFY_HEADER_START_CHECKOUT_CONFIRMATION',
                                      'NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION',
                                      /*'NOTIFY_HEADER_END_CHECKOUT_SUCCESS',*/
                                      'NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS',
                                      'NOTIFY_HEADER_START_ACCOUNT_HISTORY_INFO'));
    
        if( !($this->initialized) ) {
          $this->initialized= true;
          $this->field= -10;
        }
        $this->test_log($this->initialized ."\t=initialized at end constructor");
        $this->test_log($this->field ."\t=field at end constructor");
      }
      
      /* update method */
      function update(&$callingClass, $eventID, $paramsArray) {
        if($eventID == 'NOTIFY_HEADER_START_CHECKOUT_CONFIRMATION') {
          $this->test_log($this->initialized ."\t=initialized at start " .$eventID);
          $this->test_log($this->field ."\t=field at start " .$eventID);
          $this->field= 200;
          $this->test_log($this->initialized ."\t=initialized at end " .$eventID);
          $this->test_log($this->field ."\t=field at end " .$eventID);
        } else if($eventID == 'NOTIFY_HEADER_END_CHECKOUT_CONFIRMATION') {
          $this->test_log($this->initialized ."\t=initialized at start " .$eventID);
          $this->test_log($this->field ."\t=field at start " .$eventID);
          $this->field= 300;
          $this->test_log($this->initialized ."\t=initialized at end " .$eventID);
          $this->test_log($this->field ."\t=field at end " .$eventID);
        } else if($eventID == 'NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS') {
          $this->test_log($this->initialized ."\t=initialized at start " .$eventID);
          $this->test_log($this->field ."\t=field at start " .$eventID);
          $this->field= 400;
          $this->test_log($this->initialized ."\t=initialized at end " .$eventID);
          $this->test_log($this->field ."\t=field at end " .$eventID);
        } else if($eventID == 'NOTIFY_HEADER_START_ACCOUNT_HISTORY_INFO') {
          $this->test_log($this->initialized ."\t=initialized at start " .$eventID);
          $this->test_log($this->field ."\t=field at start " .$eventID);
          $this->field= 500;
          $this->test_log($this->initialized ."\t=initialized at end " .$eventID);
          $this->test_log($this->field ."\t=field at end " .$eventID);
        }
      }
      
      /* write debug */
      function test_log($message) {
        $flog= fopen("/tmp/zc_log.txt","a");
        fwrite($flog,$message ."\n");
        fclose($flog);
      }
    } /* end testclass */
    ?>
    As you can see the class only changes the value of the $this->field and writes simple trace to the file. But the written zc_log.txt file contains only the output from the constructor method. It seems the update method is never called.
    What is wrong here?

 

 
Page 1 of 4 123 ... LastLast

Similar Threads

  1. Can I use notifiers to assist during product edits/updates?
    By vukan71 in forum General Questions
    Replies: 4
    Last Post: 6 Aug 2012, 04:59 PM
  2. How can I use Multiple mods that use same files?
    By sfklaas in forum General Questions
    Replies: 1
    Last Post: 8 May 2009, 10:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR