Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Posts
    481
    Plugin Contributions
    0

    Default Multiple Observer Classes for Same Notifier Event

    I have two observer classes triggered by the same notifier event, but I need one observer to run before the second one. Is there a way to control the order in which observer classes are processed when triggered by the same notifier event? I wish to keep the two classes separate for code clarity as they are two completely different functions, but the second function requires data generated by the first.

    Thanks in advance!

    Dave

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Re: Multiple Observer Classes for Same Notifier Event

    I'm pretty sure that it's first-in, first-called. So you can either instantiate your classes at different configuration points (via the auto_loaders) or have them at the same configuration point in which case the config.xxx file gets run before the config.yyy file so that class.xxx gets the notification before class.yyy.

    Did that make sense?

  3. #3
    Join Date
    Jun 2012
    Posts
    481
    Plugin Contributions
    0

    Default Re: Multiple Observer Classes for Same Notifier Event

    lat9,

    Thanks for the information. With the same configuration point in the auto-loader, do you mean the class with the lowest alphabetical name goes first (xxx comes before yyy)? Is there any rule or best practice that should be followed if I change the configuration point in the auto-loader? Currently both classes are set to 90. Would setting them to 89 and 90 in the auto-loader work?

  4. #4
    Join Date
    Jun 2012
    Posts
    481
    Plugin Contributions
    0

    Default Re: Multiple Observer Classes for Same Notifier Event

    lat9,

    A little experimentation shows your suggestions are correct. The processes ran in the correct order when I set the configuration point in the auto-loader to 89 for the first class and 90 for the second. With the configuration point the same in both classes, the lowest alphabetical name ran first (the class that should have run first had a higher alphabetical name and ran second). By renaming that class and associated files so it was lower alphabetically, I got the correct class to run first. Think I'll just change the configuration point.

    Thanks again lat9!

    Dave

  5. #5
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Multiple Observer Classes for Same Notifier Event

    The configuration point only affects the order of observer class firing because the array of observers is filled in the order in which the classes are instantiated.

    The instantiation order is the key.

    And most observers (actually, most plugins in general) should be using points greater than 200. There are rare cases where something needs to fire earlier. But, those ARE indeed very rare.
    .

    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
    Jun 2012
    Posts
    481
    Plugin Contributions
    0

    Default Re: Multiple Observer Classes for Same Notifier Event

    DrByte,

    I'm confused now. Are you saying there's a difference between class firing and class instantiation? What controls the instantiation order if not the configuration point? Are both numbers in the auto-loader files configuration points? As noted above, I've set both numbers for the first class to 89, and both to 90 in the second. Those numbers will be set above 200 as you suggest.

  7. #7
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Multiple Observer Classes for Same Notifier Event

    The autoloaders "numbers" indicate the order in which all the steps are processed when loading all the mentioned files and instantiating the mentioned classes. All of that happens before any page-specific processing. It's during the page-specific processing that the notifier points are triggered ... at which point the internal list of already-loaded-and-instantiated-observer-classes is looped-through, looking for the requested notifier hook, and if a match is found then it kicks off the update() method in that corresponding observer class. Once that one is done, it continues looping through the array of observer hooks to see if the same one applies for yet another class, and kicks off that one's update() method as well, etc.

    The order in which you instantiate the classes in the autoloaders config files is the order in which they will be added to teh internal list of observer classes to process, thus also directly affecting the order in which duplicate notifier hook names will be fired.
    .

    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. Notifier/Observer problem editing Real World Example 1
    By dale88 in forum Templates, Stylesheets, Page Layout
    Replies: 23
    Last Post: 12 Mar 2010, 02:03 AM
  3. Changing multiple products at one? EG set and change same price for multiple products
    By jamesdavid in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 27 Aug 2008, 10:59 PM
  4. observer/notifier the "base" class
    By David Bo in forum Basic Configuration
    Replies: 1
    Last Post: 3 Apr 2007, 02:23 AM
  5. Is notifier/observer system good for this?
    By s_p_ike in forum General Questions
    Replies: 5
    Last Post: 7 Aug 2006, 10:16 PM

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