Jump to content

Updating quantity if item is already in cart


Go to solution Solved by requinix,

Recommended Posts

It will only update if I put items in the cart with this similar order: red, blue, blue = red:1 blue:2

But if I put: red, red = red:1 red:1

Instead of putting the quantity to 2. This only seems to be happening while adding the first 2 items of the same kind otherwise the code works fine.

$item = array(
  "image" => $image,
  "id" => $product_id,
  "price" => $price,
  "name" => $name,
  "desc" => $desc,
  "qty" => $qty,
  "slug" => $slug
);

function checkCartForItem($addItem, $cartItems) {
  if (is_array($cartItems)){
    foreach($cartItems as $key => $item) {
      if($item['name'] === $addItem)
        return $key;
    }
  }
  return false;
}

$itemExists = checkCartForItem($name, $_SESSION['cart']);

if ($itemExists){
     // item exists - increment quantity value by 1
     $_SESSION['cart'][$itemExists]['qty']++;
} else {
     // item does not exist - create new item and add to cart
     $_SESSION['cart'][] = $item;
}

Thanks

It should return 0.

I tested this as well by echoing $itemExists in my page and it in fact does return 0 when the item input is red then red.

 

Which then is inserted in line 49 to add 1 to the quantity to that specific item.

Edited by FierceViper

$itemExists will return the key or position in the array where the same product is located. In the instance of red, red being input - then 0 will be set to $itemExists.

 

I have even tried setting it so that that

if ($itemExists = "0"){

which should then should run line 49 because as stated earlier $itemExists will equal 0 for the first products of the same kind entered.

  • Solution

Oookay, so we're going further back into the basics of PHP than I was expecting.

 

0 == false. If you try to say "if (0)" then it will always execute the else branch. So if you say "if ($itemExists)" and that variable is 0 then it will also always execute the else branch.

Since your function can return 0 or false, and you want to know if the value is not literally false, then you need a literal - aka "strict" - comparison.

if ($itemExists !== false) {
On top of that is your latest post.

 

One equals sign is assignment. Two equals signs are a loose comparison, where values are compared in spirit without regard to their exact type. Three equals signs are a strict comparison, where values must be the same type for them to possibly be the same.

That if statement you have will assign the value of "0", which is a string by the way and not a number, to the variable $itemExists and then try to decide if that same value is loosely true. Which it is not. And so the else branch will execute. Again.

 

If you fixed your condition to use two equals signs for a loose comparison then it would work for items in the first position in the cart, except now every other case would be thoroughly messed up.

If you fixed your condition to use three equals signs then we'd be back to the original situation of the else executing every time, but this time it would be because the function never returns the string "0".

  • Like 1

if you simplify your cart definition, so that the key is the product_id and you are only storing the quantity in the cart, your code will be greatly simplified. you can then simply detect if an item is in the cart by just testing if the cart element with the product_id as its key isset().

 

also, it's likely that you are passing the price through the form. this is not secure, as anyone can set any of the submitted form data to anything they want. if you simply the cart as suggested and get the price and other item details from wherever they are defined on the server, when you need them, you won't have this problem.

 

so, simplify and secure your code at the same time.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.