I am trying to update a particular session variable in an array.
How i set up the session
$_SESSION['cart_items'][] = [
'size' => $size,
'color' => $color,
'qty' => $quantity,
'price' => $price,
'productId' => $productId,
'image' => $image,
'name' => $name,
];
I want to update the 'qty' when i press update in the form.
foreach ($_SESSION["cart_items"] as $key => $item) {
$item_price = $item["qty"] * $item["price"];
$total_quantity = $item["qty"];
$item_total = $item_price * $total_quantity;
$total_price = ($item["price"] * $item["qty"]);
$_SESSION['totalprice'] = $total_price;
//html code
echo '
<div >
<div id="cartimage">
<img src=' . $item['image'] . '>
</div>
<div id="cartdesc">
<form method="get" action="cart.php">
<p id="cartitemname"> ' . $item["name"] . ' </p>
<p>Quantity: <input id="updateprice" name="updateprice" type="number" step="1" min="1" value="' . $item["qty"] . '"> </p>
<p>Size: ' . $item["size"] . ' </p>
<p>Price:$ ' . $item["price"] . ' </p>
<p>Item total price $ ' . number_format($item["qty"] * $item["price"], 2) . ' </p>
<button type="submit" name="update">Update</button>
<a href="cart.php?action=remove&code=' . $key . '" ><img id="deletebtn" src="res/istockphoto-928418914-170667a.jpg" alt="Remove Item" /></a>
</form>
</div>
</div>
';
}
the get method called when i press update :
if (isset($_GET['update'])) {
//print_r($_SESSION["cart_items"]);
// what i have tried :
// $_SESSION['cart_items']['qty'] = $_GET['updateprice']
//$item["qty"] = $_GET['updateprice'];
header('location:cart.php');
}
Both of these don't work! any help or nudge in the right direction would be greatly appreciated!
CodePudding user response:
The issue is with how you add items to the cart. You're just pushing them into a indexed array, which makes it hard to reference a specific cart item after it's added.
Demo:
// Equal to how you add the cart items
$array[] = [
'id' => 1,
'qty' => 1
];
$array[] = [
'id' => 2,
'qty' => 1
];
// Results in an indexed array like this:
[
0 => [
'id' => 1,
'qty' => 1
],
1 => [
'id' => 1,
'qty' => 1
]
]
To be able to update any of them, you would need to do:
// The first
$array[0]['qty'] = 2;
// The second
$array[1]['qty'] = 2;
This makes it harder since you would need to know the index the cart item was added with. Unless you want to iterate through the cart and check each item if it is the one you want to change.
A solution would be to use an associative array instead, where you set the array key:
$array['someIdentifier] = [
'id' => 1,
'qty' => 1
];
// Results in
[
'someIdentifier' => [
'id' => 1,
'qty' => 1
]
]
Now you can access it specifically using the identifier
$array['someIdentifier']['qty'] = 2;
The identifier should probably be a unique identifier for that specific variation of the product, like size, color etc. since you could have different cart items for the same product, but with different variations.
In e-commerce systems, each variation, (or Stock Keeping Unit (SKU)), usually has a unique identifier, which would be a good identifier. Exactly how you should set them up depends on your application. It gets a bit more complicated if you consider if you should be able to change the size, color etc on an existing one. You would then need to remove the old cart item for the previous variation and add the new.
Hope it clarifies things a bit.
CodePudding user response:
Solution!! Building ontop of M. Eriksson advice
Ideally, one should have a unique Identifier in the database. Use it and replace the 'uniquekey' i wrote here. I do not have a unique key, thus the following method.
At the html form side,
<input type="hidden" name="uniquekey" id="uniquekey">
Using JS to generate a unique key:
<script>
var key = Math.random()
document.getElementById("uniquekey").value = key
</script>
Added an additional identifier, unique key
$_SESSION['cart_items'][$uniquekey] = [
'uniquekey' => $uniquekey,
'size' => $size,
'color' => $color,
'qty' => $quantity,
'price' => $price,
'productId' => $productId,
'image' => $image,
'name' => $name,
];
Now, you are able to identify the specific entry in the array!
if (isset($_GET['update'])) {
$_SESSION["cart_items"][$_GET['uniquekey']]['qty'] = $_GET['updateprice'];
}