Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2011
    Posts
    8
    Plugin Contributions
    0

    Default Using observers to modify original function

    Hello,

    I want to change the returned value of the get_products() functions from the shopping cart class. I am thinking on building something like an observer class and call the action on NOTIFIER_CART_GET_PRODUCTS_END notifier, I want to modify the returned value $products_array from the observer class so I don't mess up with the shopping cart class code directly.
    Is this possible? If so, How do I accomplish this?

    Thanks

  2. #2
    Join Date
    Jul 2012
    Posts
    16,732
    Plugin Contributions
    17

    Default Re: Using observers to modify original function

    Something like this for ZC 1.5.3 and above:

    Store this file at:
    includes/classes/observers/auto.my_observer_class.php and it will auto-load to perform the actions necessary/desired against the shopping cart class.

    There are different ways to implement the same thing. I have included both the more specific implementation for ZC 1.5.3 and above as well as some fallback code for versions previous to ZC 1.5.3 or that can still be used in current versions (update function is older, while update + CamelizedNotifier is the newer/specific style). I prefer to use the notifier per line method so that commenting out a particular notifier is easy to do.

    Code:
    <?php
    
    /**
     * @property integer $variable This is the identifier used inside the file to hold a private/non-shared piece of information
     * @author mc12345678
     *
     * For compatibility with versions < ZC 1.5.3, additional work is necessary where @TODO is found in this file.
     * Code otherwise should be functional for ZC 1.5.3 and above.
     *
     */
    
    class zcObserverMyObserverClass extends base {
    
        private $variable; // A variable that is used for internal records and use.
    
        /*
         * 
         */
        function __construct() {
    
            $attachNotifier = array();
            $attachNotifier[] = 'NOTIFIER_CART_GET_PRODUCTS_END';
    
            $this->attach($this, $attachNotifier);
    
        }
    
    
        /*
         * $this->notify('NOTIFIER_CART_GET_PRODUCTS_END', array(), $products_array);
         */
        protected function updateNotifierCartGetProductsEnd(&$callingClass, $notifier, $paramsArray, &$products_array) {
    
          // Do action here to modify whatever is in $products_array or any variables set to callingClass.  To modify anything else the 
          //    values either would have to be captured in another observer and held in this class, stored in a session, or made global 
         //    to be shared back with the global space.
        }
    
    
    
        /*
         * Generic function that is activated when any notifier identified in the observer is called but is not found in one of the above previous specific update functions is encountered as a notifier.
         */
        function update(&$callingClass, $notifier, $paramsArray) {
    
    //    $attachNotifier[] = 'NOTIFIER_CART_GET_PRODUCTS_END';
    
            if ($notifier == 'NOTIFIER_CART_GET_PRODUCTS_END') {
    //@TODO: Need to support older ZC versions that do not have the first parameter as part of the observer class.
                $this->updateNotifierCartGetProductsEnd($callingClass, $notifier, $paramsArray, $products_array);
            } //endif NOTIFY_HEADER_START_CHECKOUT_SHIPPING
    
        } //end update function - mc12345678
    } //end class - mc12345678
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

 

 

Similar Threads

  1. Copied website still using original database
    By WriteAtHome in forum General Questions
    Replies: 6
    Last Post: 4 Nov 2009, 03:47 AM
  2. Priced by attribute special using original price
    By damiantaylor in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 3 Nov 2009, 04:30 PM
  3. Using original sized images?
    By g_force in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 19 Jun 2009, 07:50 AM
  4. Using Observers instantiated from application-top.php?
    By enigmabomb in forum General Questions
    Replies: 7
    Last Post: 24 Jan 2008, 10:29 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