Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Posts
    206
    Plugin Contributions
    2

    Default Help with Auto Adding Gift to Cart

    I've managed to somewhat install a function that auto adds a gift to my cart if the cart subtotals over a certain amount by following the instructions included in the Wiki. The item will add to the cart if my cookies are cleared and the subtotal of the cart is over the set amount and will remove automatically if the cart subtotal drops below the set amount, but if I add more products to the cart to bring the total back above the set amount, the gift will not automatically add back to the cart.

    In includes/auto_loaders, I've added a file called config.freeProduct.php with this code:
    PHP Code:
    <?php
    $autoLoadConfig
    [10][] = array('autoType'=>'class',
                                  
    'loadFile'=>'observers/class.freeProduct.php');
    $autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
                                  
    'className'=>'freeProduct',
                                  
    'objectName'=>'freeProduct');
    ?>
    Then I created a file called class.freeProduct.php in includes/classes/observers with this code:
    PHP Code:
    <?php
    /**
     * Observer class used to add a free product to the cart if the user spends more than $x
     *
     */
    class freeProduct extends base {
      
    /**
       * The threshold amount the customer needs to spend.
       * 
       * Note this is defined in the shops base currency, and so works with multi currency shops
       *
       * @var decimal
       */
      
    var $freeAmount 129;
      
    /**
       * The id of the free product.
       * 
       * Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer
       * to be charged shipping on this, that you have it set correctly.
       *
       * @var integer
       */
      
    var $freeProductID 1268;
      
    /**
       * constructor method
       * 
       * Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events.
       */
      
    function freeProduct() 
      {
        global 
    $zco_notifier;
        
    $zco_notifier->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
        
    $zco_notifier->attach($this, array('NOTIFIER_CART_REMOVE_END'));
      }
      
    /**
       * Update Method
       * 
       * Called by observed class when any of our notifiable events occur
       *
       * @param object $class
       * @param string $eventID
       */
      
    function update(&$class$eventID$paramsArray = array()) {

      if (
    $eventID == 'NOTIFIER_CART_REMOVE_END' && (isset($_SESSION['freeProductInCart']) && $_SESSION['freeProductInCart'] = TRUE ))
      {
        if (!
    $_SESSION['cart']->in_cart($this->freeProductID))
        {
          
    $_SESSION['userRemovedFreeProduct'] = TRUE;
        }
      }

      if (!isset(
    $_SESSION['userRemovedFreeProduct']) || $_SESSION['userRemovedFreeProduct'] != TRUE
      {
        if (
    $_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) )   
        {
          
    $_SESSION['cart']->add_cart($this->freeProductID);
          
    $_SESSION['freeProductInCart'] = TRUE;  
        }
      }
     
      if (
    $_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) 
      {
        
    $_SESSION['cart']->remove($this->freeProductID);
        }
      }  
    }
    ?>
    I know that it's picking up the new classes because I had issues with the original coding in the wiki (I had to change
    PHP Code:
      function freeProduct() {
        
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END''NOTIFIER_CART_REMOVE_END'));
      } 
    to
    PHP Code:
      function freeProduct() 
      {
        global 
    $zco_notifier;
        
    $zco_notifier->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
        
    $zco_notifier->attach($this, array('NOTIFIER_CART_REMOVE_END'));
      } 
    and without the change, I was getting a blank screen and an error in the debugging utility).

    So I know that the problem lies somewhere after the code I changed - or maybe it has to do with the changes. At this point I'm stumped and any help would be greatly appreciated.

    Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    206
    Plugin Contributions
    2

    Default Re: Help with Auto Adding Gift to Cart

    I'm pretty sure I fixed it. It's now automatically adding and removing the gift from the cart when the total hits or goes below the target amount as well as not causing errors in the debugging utility - and all I had to do was rearrange and take out some of the function code.

    I used the same autoload file (config.freeProduct.php) but this is the code I used in includes/classes/observers/class.freeProduct.php

    PHP Code:
    <?php
    /**
     * Observer class used to add a free product to the cart if the user spends more than $x
     *
     */
    class freeProduct extends base {
      
    /**
       * The threshold amount the customer needs to spend.
       * 
       * Note this is defined in the shops base currency, and so works with multi currency shops
       *
       * @var decimal
       */
      
    var $freeAmount youramount;
      
    /**
       * The id of the free product.
       * 
       * Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer
       * to be charged shipping on this, that you have it set correctly.
       *
       * @var integer
       */
      
    var $freeProductID yourproductid;
      
    /**
       * constructor method
       * 
       * Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events.
       */
      
    function freeProduct() {
        
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
        
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_REMOVE_END'));
      }
      
    /**
       * Update Method
       * 
       * Called by observed class when any of our notifiable events occur
       *
       * @param object $class
       * @param string $eventID
       */
       
    function update(&$class$eventID$paramsArray) {
    if (
    $_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) )   
        {
          
    $_SESSION['cart']->add_cart($this->freeProductID);
          
    $_SESSION['freeProductInCart'] = TRUE;  
        }
      if (
    $_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) 
      {
        
    $_SESSION['cart']->remove($this->freeProductID);
      }
      if (
    $_SESSION['cart']->in_cart($this->freeProductID)) 
      {
        
    $_SESSION['cart']->contents[$this->freeProductID]['qty'] = 1;
      }
      }
     }
    ?>
    Just replace youramount and yourproduct id with the variables that match your site.

    Hope this helps someone out.

  3. #3
    Join Date
    Oct 2006
    Posts
    62
    Plugin Contributions
    0

    Default Re: Help with Auto Adding Gift to Cart

    Quote Originally Posted by kamelion0927 View Post

    Hope this helps someone out.
    Sure did. Thanks!

 

 

Similar Threads

  1. client says cart is auto matically adding attributes to their products
    By jill8026 in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 28 Nov 2012, 02:08 AM
  2. Help With Blank Page when adding to cart
    By jmelwak in forum Basic Configuration
    Replies: 3
    Last Post: 19 May 2009, 01:04 AM
  3. Auto pay with gift certificate or coupon
    By dealbyethan.com in forum Discounts/Coupons, Gift Certificates, Newsletters, Ads
    Replies: 0
    Last Post: 29 Mar 2008, 04:24 PM
  4. Auto-Adding product(s) to shopping cart?
    By Sheehan in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 16 Aug 2007, 05:58 PM
  5. Animated GIF with auto redirect - HELP
    By djsmyers in forum General Questions
    Replies: 3
    Last Post: 9 Feb 2007, 06:52 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