Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2006
    Posts
    4
    Plugin Contributions
    0

    Default Attributes cause in_cart and get_quantity to fail when viewing products

    Perhaps this is desired (?) - but when a product has attributes (ie, size), when a user adds to the cart - if the go back to the product page, it doesn't show how many are in the cart (because the product_id ends up as a hash) - code fix (see **MC) - obviously there's a performance hit of iterating through the product list and strcmp, but assuming carts don't get too big - i'm guessing it's not an issue:


    /**
    * Method to get the quantity of an item in the cart
    *
    * @param mixed product ID of item to check
    * @return decimal the quantity of the item
    */
    function get_quantity($products_id) {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_START');
    if (isset($this->contents[$products_id]))
    {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_QTY');
    return $this->contents[$products_id]['qty'];
    }
    else
    {
    // **mc check for attributes
    if (is_array($this->contents))
    {
    reset($this->contents);
    $total_items = 0;
    while (list($listproducts_id, ) = each($this->contents))
    {
    if(substr_compare($listproducts_id, $products_id.":", 0, strlen($products_id))==0)
    {
    $total_items += $this->get_quantity($listproducts_id);
    }
    }
    if ($total_items > 0)
    {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_QTY');
    return $total_items;
    }
    }
    // ** endmccheck
    }

    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_FALSE');
    return 0;
    }
    /**
    * Method to check whether a product exists in the cart
    *
    * @param mixed product ID of item to check
    * @return boolean
    */
    function in_cart($products_id) {
    // die($products_id);
    $this->notify('NOTIFIER_CART_IN_CART_START');
    if (isset($this->contents[$products_id]) )
    {
    $this->notify('NOTIFIER_CART_IN_CART_END_TRUE');
    return true;
    }
    else
    {
    // **mc check for attributes
    if (is_array($this->contents))
    {
    reset($this->contents);
    while (list($listproducts_id, ) = each($this->contents))
    {
    if(substr_compare($listproducts_id, $products_id.":", 0, strlen($products_id))==0)
    {
    $this->notify('NOTIFIER_CART_IN_CART_END_FALSE');
    return true;
    }
    }
    }
    // ** endmccheck
    }// end else

    $this->notify('NOTIFIER_CART_IN_CART_END_FALSE');
    return false;
    }

  2. #2
    Join Date
    Nov 2006
    Posts
    4
    Plugin Contributions
    0

    Default Re: Attributes cause in_cart and get_quantity to fail when viewing products

    Oops - spoke too soon - needs a little tweak which is now getting obscure (issue was when adding to cart - the cart looks for the quantity - i created another bug while fixing this one :) .. i supplied my code for fun - but there's def. a better way :)

    /**
    * Method to get the quantity of an item in the cart
    *
    * @param mixed product ID of item to check
    * @return decimal the quantity of the item
    */
    function get_quantity($products_id) {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_START');
    if (isset($this->contents[$products_id]))
    {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_QTY');
    return $this->contents[$products_id]['qty'];
    }
    else
    {
    // **mc check for attributes
    if (is_array($this->contents))
    {
    reset($this->contents);
    $total_items = 0;
    while (list($listproducts_id, ) = each($this->contents))
    {
    // assuming less then 10000 products
    $compareAmount = 5;
    if (strlen($products_id) < $compareAmount)
    {
    $compareAmount = strlen($products_id);
    }
    if(substr_compare($listproducts_id, $products_id.":", 0, $compareAmount+1) ==0)
    {
    $total_items += $this->get_quantity($listproducts_id);
    }
    }

    if ($total_items > 0)
    {
    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_QTY');
    return $total_items;
    }
    }
    // ** endmccheck
    }

    $this->notify('NOTIFIER_CART_GET_QUANTITY_END_FALSE');
    return 0;
    }
    /**
    * Method to check whether a product exists in the cart
    *
    * @param mixed product ID of item to check
    * @return boolean
    */
    function in_cart($products_id) {
    // die($products_id);
    $this->notify('NOTIFIER_CART_IN_CART_START');
    if (isset($this->contents[$products_id]) )
    {
    $this->notify('NOTIFIER_CART_IN_CART_END_TRUE');
    return true;
    }
    else
    {
    // **mc check for attributes
    if (is_array($this->contents) && strlen($products_id)>0)
    {
    reset($this->contents);
    while (list($listproducts_id, ) = each($this->contents))
    {
    // assuming less then 10000 products
    $compareAmount = 5;
    if (strlen($products_id) < $compareAmount)
    {
    $compareAmount = strlen($products_id);
    }
    if(substr_compare($listproducts_id, $products_id.":", 0, $compareAmount+1) ==0)
    {
    $this->notify('NOTIFIER_CART_IN_CART_END_FALSE');
    return true;
    }
    }
    }
    // ** endmccheck
    }// end else

    $this->notify('NOTIFIER_CART_IN_CART_END_FALSE');
    return false;
    }

 

 

Similar Threads

  1. Product Attributes FAIL Zen Cart/contractors FAIL - URGENT Need help, new contractor
    By PDub in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 16 Apr 2015, 07:07 AM
  2. Query Other Website When Viewing Products
    By kwright in forum General Questions
    Replies: 0
    Last Post: 29 Sep 2008, 06:47 PM
  3. Forced to sign up account when viewing products
    By scream in forum General Questions
    Replies: 15
    Last Post: 3 Sep 2008, 09:38 PM
  4. Error Message when viewing products
    By wwwursa in forum Installing on a Linux/Unix Server
    Replies: 10
    Last Post: 1 Feb 2008, 06:38 PM
  5. Error when viewing New products
    By joshlfisher in forum General Questions
    Replies: 4
    Last Post: 17 May 2007, 09:14 PM

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