codearts
codearts

Reputation: 2956

Laravel - How to change value of key inside Session array

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?

Upvotes: 1

Views: 8873

Answers (2)

codearts
codearts

Reputation: 2956

I found a solution by doing the following:

$cartItems = Session::get('cartItems', []);
foreach ($cartItems as &$cartItem) {
    if ($cartItem['product']->id == $product_id) {
        $isNewItem = false;
        if ($cartItem['size'] == $request['size']) {
             $cartItem['quantity'] += (int)$request['quantity'];
             $cartItem->save();
             Session::set('cartItems', $cartItems);
        } else {
            Session::push('cartItems', [
                'product' => $product,
                'size' => $request['size'],
                'quantity' => $request['quantity']
             ]);
        }
    }
}

I found this solution by doing some digging. Here is the question.

Upvotes: 0

Denis Mysenko
Denis Mysenko

Reputation: 6534

In your loop, $cartItem is a temporary loop variable - changing it doesn't affect the session value. Or in other words, you are not updating your session when the sizes are equal.

Laravel has a convenience method push() for pushing to arrays - which you are using already, but unfortunately there is no array "update" method, so you would need to fetch the whole cart, update it as necessary and then set it again:

$cartItems = Session::get('cartItems');
$newItems = [];    

foreach ($cartItems as $cartItem) {
        if ($cartItem['product']->id == $product_id && $cartItem['size'] == $request['size']) {
                $cartItem['quantity'] += (int)$request['quantity'];
        } else {
                $newItems[] = [
                        'product' => $product,
                        'size' => $request['size'],
                        'quantity' => $request['quantity']
                ];
        }
}

Session::put('cartItems', $cartItems + $newItems);

Upvotes: 2

Related Questions