Search code examples
phpoopshopping-cart

Php add to shopping cart problem


Im trying to create a php function that adds an item to a shopping cart. what i want it to do is check the array to see if the item is already in there, if it is increase the quantity, if not create the item in the cart.

What it is doing instead is it adding an item, it'll work the first time (if the items already there it'l just increase the quantity) but if you add another item it keeps on creating new instances of that item in the shopping cart e.g

item 1 - quantity 4 item 2 - quantity 1 item 2 - quantity 1 item 2 - quantity 1... and so on...

below is the code i have so far?

function add_item ($id, $qty)
    {
        $count=$this->countItems;
        echo  "uytfdgghjkl;kj<br>";
        $added = false;
        if($count>0)
        {
            $i=0;
            while($added == false)
            {
                echo "fghjkl<br>";
                $tid = $this->items[$i]->getId();
                echo "new ID: ".$tid."<br>";
                echo "old ID: ".$id."<br>";
                echo $i;
                if($tid == $id)
                {
                    $amount = $this->items[$i]->getQty();
                    $this->items[$i]->setQty($amount+1);
                    $added = true;
                    //$i++;
                    //break;
                }
                if($added == true)
                {
                    break;
                }
                else //if($added == false)
                {
                    $this->items[$this->countItems] = new OrderItem($id, $qty);
                    //$this->total = $total+ ($qty *$price);
                    $this->countItems++;
                    $added = true;
                    //break;
                }
                //else break;
                $i++;
            }

        }
        else
        {
            $this->items[$this->countItems] = new OrderItem($id, $qty);
            //$this->total = $total+ ($qty *$price);
            $this->countItems++;
        }
    }

Solution

  • The problem is that you aren't searching the whole array first to see if the item is present in it. The code below should work, but I may have made a typo or something else so make sure you double check it.

    function add_item ($id, $qty)
        {
            $count=$this->countItems;
            echo  "uytfdgghjkl;kj<br>";
            $added = false;
            if($count>0)
            {
                for($i=0; $i < $count; $i++)
                {
                    echo "fghjkl<br>";
                    $tid = $this->items[$i]->getId();
                    echo "new ID: ".$tid."<br>";
                    echo "old ID: ".$id."<br>";
                    echo $i;
                    if($tid == $id)
                    {
                        $amount = $this->items[$i]->getQty();
                        $this->items[$i]->setQty($amount+1);
                        $added = true;
                        break;
                    }
                }
    
            }
            if(!$added)
            {
                $this->items[$this->countItems] = new OrderItem($id, $qty);
                //$this->total = $total+ ($qty *$price);
                $this->countItems++;
            }
        }
    

    An even better option would be to use a dictionary

    ie.

    $arr = array();
    $arr['item_id'] = new OrderItem(...);
    

    Then you can check if the item is in the array using:

    if(isset($arr[$id])){
        ...
    }