Page 1 of 1

need help with shoppingcart class

Posted: Sat Apr 18, 2009 6:44 am
by thunderstorm
Hi, I have a class created for a shopping cart which works quite well. I have buttons next to products to add items. When you click the button, it adds it fine. But when you click again, nothing happens. Ideally, I want it to just increment the quantity of that item already in the cart.

I have a 'view basket' page that allows users to change the quantities in the cart - this works fine, so a bit confused why I can't get the first bit working.

My class is as follows:

Code: Select all

class ShoppingCart
{
    // collection of items placed in the shopping cart
    private $items;
 
    // initialize a ShoppingCart object
    public function __construct()
    {
        $this->items = array();
    }
 
    // expose read-only convenience properties
    public function __get($value)
    {
        switch ($value)
        {
            // contents - returns the entire contents of the cart
            case 'contents':
                return $this->items;
                brake;
    
            // isEmpty - returns whether or not the cart is empty
            case 'isEmpty':
                return (count($this->items) == 0);
                break;
    
            // totalItems - returns the total number of distinct items
            // in the cart
            case 'totalItems':
                return count($this->items);
                break;
    
            // totalQty - returns the total quantity of items in the cart
            case 'totalQty':
                return array_sum($this->items);
                break;
         }
    }
 
    // add an item to the shopping cart    
    public function addItem($item, $qty)
    {
        if (!$qty)
        {
            $this->removeItem($item);
        }
        else
        {
            if (!in_array($item,$this->items)) {
                $this->items[$item] = $qty;
            } else {
[b]                $currentqty= $this->items[$item];[/b]
              [b]  $newqty = $currentqty + 1;[/b]
            [b]    $this->items[$item] = $newqty;[/b]
            }
        }
    }
 
    // returns an item's quantity in the cart 
    public function qtyItem($item)
    {
        if (!isset($this->items[$item]))
        {
            return 0;
        }
        else
        {
            return $this->items[$item];
        }
    }
}
When a user clicks a button to add an item, the following code gets called from a page called cart.php which handles all cart operations:

Code: Select all

       // add item to cart
        if (isset($_GET['add']))
        {
            if(isset($_GET['qty'])) {
                $cart->addItem($item, $_GET['qty']);
            }
            else {
                $cart->addItem($item);
            }
        }
However, when updating quantities from the view basket page, a query string is passed in the format qty[itemid] = qty to the same cart.php page, and this bit of code updates the quantities:

Code: Select all

   // update item quantities in shopping cart
    if (isset($_GET['update']))
    {
        foreach ($_POST['qty'] as $item => $qty)
        {
            $cart->addItem($item, $qty);
        }
    }
A friend helped me write this code a while ago and only just realsed I need to change the buttons to add products so clicking it twice would add it, then second click would increment the quantity etc etc.

I thought I could just change the addItem method in the class itself, I've done a check to see if the item already exists (if so just add it as normal) and this seems to work, as falls through to the 'else if' it does already exist, but I can't seem to increase the quantity- it stays at 1! I've highlighted the code in the class code section that isn't working.

Sorry for this very long post, but if anyone has any ideas would be grateful as struggling to really understand whats going on here.

Thanks!
Stef

Re: need help with shoppingcart class

Posted: Mon Apr 20, 2009 10:08 am
by McInfo
1. How are you storing the shopping cart data between pages?

2. Are you sure

Code: Select all

$cart->addItem($item, $qty);
is being called in your update item script?

You can add an echo to your addItem() method to confirm that it was called.

Code: Select all

// ...
public function addItem($item, $qty)
{
    echo "\n{" . $item . ':' . $qty . "}\n";
    // ...
}
// ...
Edit: This post was recovered from search engine cache.