Update Shopping Cart

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?

its simple, set the array key to the product _id and the array value to the quantity. Example :

// product_id is 1234 and quantity is 1
$cart = [
    1234 => 1
];

// add two other products with product_id 1234:
$product_id = 1234;
$quantity = 2;

if(isset($cart[$product_id])) {
    // in case there is already a key 1234
    $cart[$product_id] += $quantity;
} else {
   // if there is no key 1234 yet
   $cart[$product_id] = $quantity;
}

print_r($cart);

Result:

Array
(
    [1234] => 3
)

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”]);

Programming is a proactive activity. It is not a reactive activity. What this means is, if your code doesn’t produce the correct result, you find and fix the problem causing the wrong result. You don’t go, oh well, there’s an extra piece of data after I run some code, time to add more code to remove the extra data.

This simple cart definition that you were originally given here on phphelp.com and that you specifically asked about at the top of this thread, uses the item id as the array index. This is so that you can directly access items in the cart by their id. Array indexes are unique, so, it is impossible for there to be more than one entry in the array with the same item id. The code that’s been posted in the replies in the different help forums (3 that I know of) contains the same fundamental logic. You have been told/shown three times how you should do this.

1 Like

Do you think it could be something wrong with my original array? This is how I define $cart_items

for($i=0; $i<=5; $i++){
	
if (isset($_REQUEST["element_id_$i"]) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];	
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
	
if (isset($_REQUEST["quantity_$i"])) {
$_SESSION["quantity_$i"] = $_REQUEST["quantity_$i"];
$quantity = $_SESSION["quantity_$i"];

array_push($_SESSION["quantity"],$quantity);
	}
$quantity = $_SESSION["quantity"];
	
$_SESSION["cart_item"] = array(
"cart_item" => array(
"id" => $id,
"quantity" => $quantity
));

}
$cart_items = $_SESSION["cart_item"];

About four threads ago, for this same subject, you posted a description/question about what sounded like an overly complicated attempt at doing this. It was at this point that a simple cart array definition was given - because it would result in the simplest code. Each of my replies in that thread, and one following thread, used a form of the word ‘simple’ in it. The multiple, similar, code examples you got for this current question take just three php statements (one conditional test and two assignment statements) to accomplish inserting a new item quantity or updating an existing item quantity. How about learning from and using the information you have being given in the replies?

Sponsor our Newsletter | Privacy Policy | Terms of Service