PHP Shopping Cart Update Basket Item

Hey,

I am just finishing off my shopping cart however i have stumbled upon a problem.

When i add to basket i execute the following method:


    public function AddToCart(){
        !isset($_SESSION['ID']) ? $_SESSION['ID'] = array() : '';
        !isset($_SESSION['theName']) ? $_SESSION['theName'] = array() : '';
        !isset($_SESSION['quantity']) ? $_SESSION['quantity'] = array() : '';
        !isset($_SESSION['price']) ? $_SESSION['price'] = array() : '';
        !isset($_SESSION['image']) ? $_SESSION['image'] = array() : '';

        array_push($_SESSION['ID'], $_POST['ID']);
        array_push($_SESSION['theName'], $_POST['theName']);
        array_push($_SESSION['quantity'], $_POST['quantity']);
        array_push($_SESSION['price'], $_POST['price']);
        array_push($_SESSION['image'], $_POST['image']);
        $loc = $_SERVER['HTTP_REFERER'];
        echo "<script>window.location.href='".$loc."'</script>";
    }

This creates the session as an array. Now lets say i click on the add to basket button twice for the same item. I don’t want to have 2 instances of the same item which is what i have not, but i want to have the item name once and then ‘x2’ next to the quantity.

So moving on on my items page i have hidden input values which populates the basket session, shown below:


        <input type="hidden" name="ID" value="<?php echo $row['theID']; ?>" />
        <input type="hidden" name="theName" value="<?php echo $row['theName']; ?>" />
        <input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
        <input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
        <input type="hidden" name="quantity" value="1" />

Now what i have managed to do is when my basket is empty and i add an item to the basket multiple times the basket works exactly how i want. However when i then add a different item to the basket more than once it adds a quantity to both items.

This is because i need to target the specific item in the basket.


    if(isset($_POST['ID'])){
        if(!empty($_SESSION['theName'])){
           $i=0;
            foreach($_SESSION['theName'] as $name){
                if($_POST['theName'] == $name){
                    $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
                }else {
                    $cart->AddToCart();
                }
            }
        }
    }

I tested this code and clicked add to cart multiple times on 2 different items. I get this:

Dior - £69.99 x4
CK Top - £1.99 x1
CK Top - £1.99 x1

Now this item:

Dior - £69.99 x4

was actually x3 but when i clicked on a different item more than once it updated both items quantities.

So my question is, can i target a specific item in a session array and accomplish what i am trying to do?

I would really appreciate if you can help me out.

Thanks

Hey,

Thanks so much. That works now :wink:

I really appreciate your assistance.

Thanks again

By the way i have managed to find one more glitch in regards to the basket. I’m sure it’s a similar approach but just need re-assuring. In my basket if i have 5 quantities of the same item and i click on remove-item, i want it to decrement one from the quantity and not all of the items totally like it is doing now.

So I have a remove-item link and it does this:


      if(!empty($_SESSION['ID'])) {
        foreach($_SESSION['ID'] as $i => $id) {
          if($_GET['ID'] == $id && $_SESSION['quantity'][$i] > 1) {
            $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] - 1;
          } else {
          $result = $cart->removeItem();
          }
        }
      }

But it keeps going straight to this $result = $cart->removeItem(); which removes the full item from the basket.

Any ideas what i’m doing wrong?

Thanks

And when i put the else statement back in it add a quantity to both of the items in the cart… so it’s not quite right :confused:

Any ideas what i’m doing wrong?

Ah yes, when you have items in the cart the code assumes the new product is one of those.
You should do it like this


if(isset($_POST['ID'])) {
  $found = false;
  if(!empty($_SESSION['ID'])) {
    foreach($_SESSION['ID'] as $i => $id) {
      if($_POST['ID'] == $id) {
        $found = true;
        $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
        break;
      }
    }
  }
  if (!$found) {
    $cart->AddToCart();
  }
} 

Try to understand this code, it’s not trivial.
Any questions, please ask :slight_smile:

Hey,

Thanks, i did think of this actually and have just tried it now. However what it is doing now is this;

When the basket is empty and i add something it works. I can add the same item multiple times. But when i now try to add a different item to the basket it doesn’t add to the basket.

I think this is because in the IF statement i am saying IF $_SESSION[‘ID’] in not empty add a quantity ELSE add to cart.

So when it comes to adding a different item to the cart it checks to see it $_SESSION[‘ID’] is empty which is isn’t and then does not add to cart?

Is this what the problem is?

Can i fix this?

I think i will post another thread on this as it relates to a different section of the website.

Thanks again

yes, you should remove the else part of the if statement in the foreach loop:


if(isset($_POST['ID'])) {
  if(!empty($_SESSION['ID'])) {
    foreach($_SESSION['ID'] as $i => $id) {
      if($_POST['ID'] == $id) {
        $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
      }
      // there used to be an "else" statement here, but no anymore
    }
  } else {
    $cart->AddToCart();
  }
}

Hey,

Thanks! And i know about the hidden fields problem, i have been told off about this a lot today :frowning: I didn’t know that the value of the price could be changed in firebug.

I will test this out on websites hehe :goof:

Anyway that code nearly worked for me. I will show you what happened.

I firstly clicked on some items and added them to the basket, see this image:

http://www.glofamily.com/glo/images/step-1.jpg

At this point i had 2 items in the basket, one with a quantity of 2. Now i then added “King Stud” again and it did this:

http://www.glofamily.com/glo/images/step-2.jpg

It added the quantity to the item perfectly but then added another instance of the item to the session.

Any ideas?

Thanks again

DO NOT use hidden fields for prices! Use a hidden field for the ID only, and get the price from the database when the form is posted!
People can change the HTML (using firebug for example) and change the price of a product! They can set it to cost $0.01 for example and your code doesn’t care!

Your code should be changed as follows:


    if(isset($_POST['ID'])){
        if(!empty($_SESSION['theName'])){
           // remove $i=0;
            foreach($_SESSION['theName'] as $i => $name){ // add $i =>
                if($_POST['theName'] == $name) {
                    $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
                }else {
                    $cart->AddToCart();
                }
            }
        } else {
           $cart->AddToCart();
        }
    }

Also, if the products have ID’s, don’t check on the name, as the name of a prdocuct might not be unique, but the id must be unique.

So:


    if(isset($_POST['ID'])){
        if(!empty($_SESSION['ID'])){
           // remove $i=0;
            foreach($_SESSION['ID'] as $i => $id){ // add $i =>
                if($_POST['ID'] == $id){
                    $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
                }else {
                    $cart->AddToCart();
                }
            }
        } else {
           $cart->AddToCart();
        }
    }

Hey,

Any advice people??

When i do this:


    if(isset($_POST['ID'])){
       if(!empty($_SESSION['theName'])){
           $i=0;
            foreach($_SESSION['theName'] as $name){
                if($_POST['theName'] == $name){
                    $_SESSION['quantity'][$i] = $_SESSION['quantity'][$i] + 1;
                }else {
                    $cart->AddToCart();
                }
            }
        } else {
            $cart->AddToCart();
        }
    }

I was thinking of adding a counter and checking to see if it matches the foreach and then adding one to the quantity. What this code currently does is shown in this image:

http://www.glofamily.com/glo/images/basket.jpg

So when i clicked on “King Stud” twice it worked but then when i clicked on a different item it stops working.

King Stud should show as x3.

Any ideas what i need to do?

Thanks again

Just to be clear, i need to in essence update the quantity of the item within the session array.

So for example if i add 3 items to my basket i will have 3 items in my array, would i be able to alter the second item in the array?

Thanks