FierceViper Posted April 17, 2018 Share Posted April 17, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/ Share on other sites More sharing options...
requinix Posted April 17, 2018 Share Posted April 17, 2018 If checkCartForItem finds the item as the first one in the cart, what is the value it will return? Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557913 Share on other sites More sharing options...
FierceViper Posted April 17, 2018 Author Share Posted April 17, 2018 (edited) 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 April 17, 2018 by FierceViper Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557914 Share on other sites More sharing options...
requinix Posted April 17, 2018 Share Posted April 17, 2018 You're half right. That's what it will return, but line 49 will not execute. Line 52 will. Look at the if on line 47. if ($itemExists){Remind me what the value of $itemExists will be again? Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557915 Share on other sites More sharing options...
FierceViper Posted April 17, 2018 Author Share Posted April 17, 2018 $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. Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557916 Share on other sites More sharing options...
Solution requinix Posted April 18, 2018 Solution Share Posted April 18, 2018 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". 1 Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557917 Share on other sites More sharing options...
FierceViper Posted April 18, 2018 Author Share Posted April 18, 2018 You are correct, I am pretty new to PHP. After your explanation this is starting to make sense now and checking if it is not false does work. Thanks for being patient with me and also thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557918 Share on other sites More sharing options...
mac_gyver Posted April 18, 2018 Share Posted April 18, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307151-updating-quantity-if-item-is-already-in-cart/#findComment-1557919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.