makamo66 Posted November 8, 2019 Share Posted November 8, 2019 If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product? Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/ Share on other sites More sharing options...
Barand Posted November 8, 2019 Share Posted November 8, 2019 Like this example in this topic 1) Read your replies 2) Don't repeatedly post the same question. Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571349 Share on other sites More sharing options...
makamo66 Posted November 9, 2019 Author Share Posted November 9, 2019 I'm doing something like that. My quantity is updating when I add the same product but I need to delete the product that's the last item in the SESSION array. So far I've tried array_pop and unset but I can't get rid of the non-updated product. These two don't work to remove the last product added to the session: unset($_SESSION["cart_item"]["element_id"]); array_pop($_SESSION["cart_item"]); Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571402 Share on other sites More sharing options...
Barand Posted November 9, 2019 Share Posted November 9, 2019 3 minutes ago, makamo66 said: but I need to delete the product that's the last item in the SESSION array Why? Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571403 Share on other sites More sharing options...
makamo66 Posted November 9, 2019 Author Share Posted November 9, 2019 The last product in the SESSION array is the product with the non-updated quantity. When I update the quantity, I get the previous product and also the updated product. I have to remove the non-updated product. Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571407 Share on other sites More sharing options...
Barand Posted November 9, 2019 Share Posted November 9, 2019 30 minutes ago, makamo66 said: I'm doing something like that Doesn't sound like you are doing anything like that. In my code, if the product isn't in the cart it adds it, with the quantity. If the product is already in the cart it updates the quantity. No removals required (unless, of course, the user wants to cancel) Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571408 Share on other sites More sharing options...
makamo66 Posted November 9, 2019 Author Share Posted November 9, 2019 This is what I'm using and it's not working: foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ if(empty($item["quantity"])){ $item["quantity"] = $value; } elseif (isset($_REQUEST["quantity_$i"]) ) { $item["quantity"] = $value + $_REQUEST["quantity_$i"]; $value = $item["quantity"]; echo "Same product"; }}}} Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571411 Share on other sites More sharing options...
NotSunfighter Posted November 9, 2019 Share Posted November 9, 2019 (edited) It looks to me that your making things harder than they should be. Here's what I would do for a cart array: <?php $cart_items = array("pants"=>"3", "shirt"=>"4", "shoe"=>"5", "banana"=>"13", "hat"=>"1"); foreach ($cart_items as $item => $quantity){ echo "Item name is ".$item." and he wants this amount ".$quantity."\n"; } ?> Edited November 9, 2019 by NotSunfighter Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571418 Share on other sites More sharing options...
NotSunfighter Posted November 9, 2019 Share Posted November 9, 2019 And I'd remove objects from the array like this <?php $cart_items = array("pants"=>"3", "shirt"=>"4", "shoe"=>"5", "banana"=>"13", "hat"=>"1"); if (($key = array_search('4', $cart_items)) !== false) { unset($cart_items[$key]); } foreach ($cart_items as $item => $quantity){ echo "Item name is ".$item." and he wants this amount ".$quantity."\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571419 Share on other sites More sharing options...
Barand Posted November 9, 2019 Share Posted November 9, 2019 @NotSunfighter He was shown exactly how to do it in a previous topic of his in this forum. Not worth wasting any more time. Quote Link to comment https://forums.phpfreaks.com/topic/309487-update-shopping-cart/#findComment-1571421 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.