Zen Cart Logo
Forums / Bug Reports / Attributes cause in_cart and get_quantity to fail when viewing products

Attributes cause in_cart and get_quantity to fail when viewing products

Locked

Views: 1,957

Results 1 to 2 of 2
This thread is locked. New replies are disabled.
7 Nov 2006, 8:53 AM
#1
psuskeels avatar

psuskeels

New Zenner

Join Date:
Nov 2006
Posts:
4
Plugin Contributions:
0

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

7 Nov 2006, 10:18 AM
#2
psuskeels avatar

psuskeels

New Zenner

Join Date:
Nov 2006
Posts:
4
Plugin Contributions:
0

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;

}