So I am building a cart and I am using Session to store data for guest users. Here is how I store the items.
Session::push('cartItems', [
'product' = > $product,
'size' = > $request['size'],
'quantity' = > $request['quantity']
]);
If a guest user adds an item with the same size it should only add the chosen quantity to the cartItem['quantity']. Here is how I do that:
foreach(Session::get('cartItems') as $cartItem) {
if ($cartItem['product'] - > id == $product_id) {
$isNewItem = false;
if ($cartItem['size'] == $request['size']) {
$cartItem['quantity'] += (int) $request['quantity'];
} else {
Session::push('cartItems', [
'product' = > $product,
'size' = > $request['size'],
'quantity' = > $request['quantity']
]);
}
}
}
When I try to add a product when a product with the same size already exists in the cart it goes through that part of the code
if ($cartItem['size'] == $request['size']) {
$cartItem['quantity'] += (int) $request['quantity'];
}
But the quantity of the $cartItem doesn't change at all. How can I change it?