Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2010
    Posts
    11
    Plugin Contributions
    0

    Default Observer Noob help?

    Hey guys, trying to get my first Observer working.

    I have placed the following in includes/auto_loaders/config.test_ordernum.php:
    PHP Code:
    $autoLoadConfig[0][] = array('autoType'=>'class',
                                  
    'loadFile'=>'observers/class.test_observer.php');
    $autoLoadConfig[0][] = array('autoType'=>'classInstantiate',
                                  
    'className'=>'test_ordernum',
                                  
    'objectName'=>'test_ordernum'); 
    And I have placed the following in include/classes/observers/class.test_observer.php:

    PHP Code:
    class test_ordernum extends base {

        function 
    test_ordernum() {
            
    $this->attach($this, array('NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE'));
        }
            
        function 
    update(&$class$notifier$paramsArray) {
            global 
    $db;
            
    $sql "INSERT INTO test_ordernum ( orders_id, ordernum) VALUES ('55','test')";
            
    $result $db->Execute($sql);
        }

    Now, I know for sure that the file is loading, because if I put some jacked up syntax in it, it errors out.

    However, my problem is that that query is never running. I'm under the assumption that that is literally all I need in my class -- update should automatically be called when the notificaton is triggered.

    Maybe it has something to do with the autoload "breakpoint"? I tried various levels, but no luck. Maybe theres some obvious syntax or reference issue (php is not forte, but I am in experienced in other languages)

    Long story short, what I'm trying to do is create an alphanumeric order numer thats based on order date, and due to interfacing with a third party system, it is just easier to store it, instead of doing the display-hack that I've seen a lot of order number modifications do.

    Any help is appreciated!

  2. #2
    Join Date
    Mar 2010
    Posts
    11
    Plugin Contributions
    0

    Default Re: Observer Noob help?

    <jayz>Can I get a help help?</jayz>

  3. #3
    Join Date
    Mar 2010
    Posts
    11
    Plugin Contributions
    0

    Default Re: Observer Noob help?

    Still looking for a little help on this issue, if anyone can provide it!

  4. #4
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Observer Noob help?

    While I would recommend using a breakpoint of 90 or higher, I see nothing wrong with that code, and in fact when I just tested it now it works fine for me. Well, I say "fine" because I know it's attempting the query because I get this back:
    1146 Table 'my_database_name.test_ordernum' doesn't exist
    in:
    [INSERT INTO test_ordernum ( orders_id, ordernum) VALUES ('55','test')]
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  5. #5
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Observer Noob help?

    I tested it on a development copy of the not-yet-released v139 ... and it works fine.
    But it doesn't fire properly on 1.3.8.

    So, as a workaround, you'll need to replace the contents of your /includes/classes/class.base.php file with the following to make it work on 1.3.8:
    Code:
    <?php
    /** 
     * File contains just the base class
     *
     * @package classes
     * @copyright Copyright 2003-2009 Zen Cart Development Team
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     * @version $Id: class.base.php 14535 2009-10-07 22:16:19Z wilt $
     */
    /**
     * abstract class base
     *
     * any class that wants to notify or listen for events must extend this base class
     *
     * @package classes
     */
    class base {
      /**
       * method used to an attach an observer to the notifier object
       * 
       * NB. We have to get a little sneaky here to stop session based classes adding events ad infinitum
       * To do this we first concatenate the class name with the event id, as a class is only ever going to attach to an
       * event id once, this provides a unigue key. To ensure there are no naming problems with the array key, we md5 the unique
       * name to provide a unique hashed key. 
       * 
       * @param object Reference to the observer class
       * @param array An array of eventId's to observe
       */
      function attach(&$observer, $eventIDArray) {
        foreach($eventIDArray as $eventID) {
          $nameHash = md5(get_class($observer).$eventID);
          base::setStaticObserver($nameHash, array('obs'=>&$observer, 'eventID'=>$eventID));
        }
      }
      /**
       * method used to detach an observer from the notifier object
       * @param object
       * @param array
       */
      function detach($observer, $eventIDArray) {
        foreach($eventIDArray as $eventID) {    
          $nameHash = md5(get_class($observer).$eventID);
          base::unsetStaticObserver($nameHash);
        }
      }
      /**
       * method to notify observers that an event as occurred in the notifier object
       * 
       * @param string The event ID to notify for
       * @param array paramters to pass to the observer, useful for passing stuff which is outside of the 'scope' of the observed class.
       */
      function notify($eventID, $paramArray = array()) {
        $observers = & base::getStaticObserver();
        if (!is_null($observers))
        {
          foreach($observers as $key=>$obs) {
            if ($obs['eventID'] == $eventID) {
              $obs['obs']->update($this, $eventID, $paramArray);
            }
          }
        }
      }
      function & getStaticProperty($var)
      {
        static $staticProperty;
        return $staticProperty;
      }
      function & getStaticObserver() {
        return base::getStaticProperty('observer');
      }
      function & setStaticObserver($element, $value)
      {
        $observer =  & base::getStaticObserver();
        $observer[$element] = $value;
      }
      function & unsetStaticObserver($element)
      {
        $observer =  & base::getStaticObserver();
        unset($observer[$element]);
      }
    }
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  6. #6
    Join Date
    Mar 2010
    Posts
    11
    Plugin Contributions
    0

    Default Re: Observer Noob help?

    That did the trick! Thanks a ton!

    Is it not fully released for 1.3.8? I mean, why did that file need to be updated, for it to work? (Just curious)

    Also, holy crap @ number of posts!

  7. #7
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Observer Noob help?

    Quote Originally Posted by absurdparadox View Post
    That did the trick! Thanks a ton!
    You're welcome.
    Quote Originally Posted by absurdparadox View Post
    Is it not fully released for 1.3.8? I mean, why did that file need to be updated, for it to work? (Just curious)
    I found some bugs with it when I was testing some things while developing 1.3.9, and so we looked at what might be causing it and found some flaws in the original design of the notifier/observer stuff in the base class. So, we rolled those into the upcoming 1.3.9. The stuff I posted has not been extensively tested on 1.3.8, but I know it works for the case you mentioned above. I can't speak to other uses, but I would be surprised if there were any adverse side-effects. It will not be released officially for 1.3.8 though. But it will be in 1.3.9. You're welcome to use it on your site in the meantime.
    Quote Originally Posted by absurdparadox View Post
    Also, holy crap @ number of posts!
    LOL -- well, we don't just write the software, we support it too ;)
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Observer Notifier NOTIFIER_CART_GET_PRODUCTS_END Help
    By Celtic in forum General Questions
    Replies: 2
    Last Post: 9 Jun 2011, 11:22 PM
  2. Noob help
    By royaltees in forum Templates, Stylesheets, Page Layout
    Replies: 7
    Last Post: 14 Oct 2008, 11:05 AM
  3. Replies: 21
    Last Post: 17 Feb 2008, 05:59 PM
  4. Noob help
    By gjanicoman in forum Templates, Stylesheets, Page Layout
    Replies: 8
    Last Post: 10 Nov 2007, 05:04 AM

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