Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    7
    Plugin Contributions
    0

    Default class.freeProduct.php Problem - completely stumped

    OK, I am using the class.freeProduct.php real world example from zen wikipedia.

    I have everything set up and it is working fine, adding a free product to the cart as expected when a total reaches a certain amount.

    It also removes the free product if the user falls beaneath the required amount.

    All cool, right?

    But I am ready to throw my computer against the wall, because I cannot get this code to properly remove the item if a user simply doesn't want the free gift.

    If a user clicks on the remove item on the shopping cart, it reloads the page and the item is still in there.

    I cannot for the life of me figure out how to get this to function properly and remove the free product, and I have been at this for over 5 hours now.

    Someone smarter than me, please come to my rescue - thank you!

  2. #2
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,925
    Plugin Contributions
    4

    Default Re: class.freeProduct.php Problem - completely stumped

    k,

    The problem here is that the observer update is firing whenever an item is removed from the cart. It then looks at the total and determines whether to add the free product to the cart. It will do this even if the product being removed is the free product itself.

    On way to get around this,

    In your observer code where it adds the free product to the cart, set a session variable.

    i.e. $_SESSION['freeProductInCart'] = TRUE;

    Now at the beginning of the your observer update method you will need to test

    1) that the eventID = NOTIFIER_CART_REMOVE_END
    2) if 1) is true test that $_SESSION['freeProductInCart'] is set and is TRUE
    3) if 2) is TRUE test if the free product is still in the cart
    i.e. if ($_SESSION['cart']->in_cart($this->freeProductID))

    if those tests pass, then you know the user has removed the free product, so we then set another session variable

    $_SESSION['userRemovedFreeProduct'] = TRUE;

    you can then use that to selectively run the code that adds the free product to the cart
    Code:
    ie 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);
        }
    }
    n.b. it would be better if some of our notifiers passed some additional detail where appropriate, ie life would have been easier if NOTIFIER_CART_REMOVE_END passed the product id of the item being removed. its on the todo list :)

  3. #3
    Join Date
    Aug 2009
    Posts
    7
    Plugin Contributions
    0

    Default Re: class.freeProduct.php Problem - completely stumped

    Thank you so much for the response - I follow it clearly for the most part, except that when I add the code to the class.freeProduct.php it is not working (so maybe I am not following it so clearly??? :))

    My code is as follows:


    function freeProduct() {
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', '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) {



    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 (!isset($_SESSION['userRemovedFreeProduct']) || $_SESSION['userRemovedFreeProduct'] != 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;
    }
    }
    }




  4. #4
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,925
    Plugin Contributions
    4

    Default Re: class.freeProduct.php Problem - completely stumped

    I'm not sure you did follow it that clearly

    He's what I was getting at in more detail, note I have not tested this at all. !!!

    PHP Code:
    function update(&$class$eventID) {

      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);
      }

      if (
    $_SESSION['cart']->in_cart($this->freeProductID)) 
      {
        
    $_SESSION['cart']->contents[$this->freeProductID]['qty'] = 1;
      }
        


  5. #5
    Join Date
    Aug 2009
    Posts
    7
    Plugin Contributions
    0

    Default Re: class.freeProduct.php Problem - completely stumped

    k, I took the code you have and added it to the page. Product still not deleting. I was trying to walk through the code line by line to figure out what each one does, and I do understand when it is being set to remove and add, etc. When I added in just the code you have here, same thing happens - the product remains in the cart.

    So I added the following to the class:

    if ($_SESSION['userRemovedFreeProduct'] == TRUE){
    $_SESSION['cart']->remove($this->freeProductID);
    }

    And now, when I click on the item in the shopping cart to remove it, the page load time is high, then I get a page not found error.

    Sorry - this is the first time I've been this stumped at a zen cart issue. I am absolutely clueless as to how to get this to work and my head is spinning.

  6. #6
    Join Date
    Aug 2009
    Posts
    7
    Plugin Contributions
    0

    Default Re: class.freeProduct.php Problem - completely stumped

    Never mind - you are a genius and I thank you for saving me from taking 400 motrins.

    I simply reverted back to the previous class that I had, and took your code once again and added it to the page and it works flawlessly.

    Thank you, thank you for saving me many more hours of work on this. I shall buy you a cup of coffee. :)

 

 

Similar Threads

  1. v151 Stumped Override for includes / languages/ CUSTOM / header.php
    By ChadAustin in forum Templates, Stylesheets, Page Layout
    Replies: 3
    Last Post: 14 Jan 2013, 07:39 PM
  2. stumped.. really stumped Error 500...again
    By rwslippey in forum Upgrading from 1.3.x to 1.3.9
    Replies: 1
    Last Post: 1 Jul 2010, 02:40 AM
  3. Problem with install - cannot instantiate class - autoload_func.php
    By msd in forum Installing on a Linux/Unix Server
    Replies: 30
    Last Post: 5 Jan 2008, 12:04 AM
  4. CSS Problem Stumped
    By mboley370 in forum Templates, Stylesheets, Page Layout
    Replies: 1
    Last Post: 16 May 2007, 03:51 AM
  5. A Simple Problem but I'm Stumped!!!
    By HandcraftedUK in forum General Questions
    Replies: 6
    Last Post: 23 Nov 2006, 02:06 AM

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