Rajat Modi
Rajat Modi

Reputation: 1343

Update node of session array + zf2

First I added a session then I print my session it looks completely ok here it is

Array
(
[14] => Array//(main key is my restaurant Id)
    (
        [retaurantDetail] => Array
            (
                [restId] => 14
                [restaurantName] => Barca
                [published] => 1
                [timings] => 10 to 10
                [normalCost] => 150
                [logo] => 44f7afcffb0aeea5c69ccee9041cab84.jpg
                [email] => [email protected]
                [phone] => 741258
            )

        [menuArray] => Array
            (
                [70] => Array // (menu ID is the Key)
                    (
                        [menuId] => 70
                        [productId] => 35
                        [productName] => Coca Cola
                        [categoryTitle] => Beverages
                        [categoryId] => 52
                        [price] => 100
                        [attributeName] => 1.5L
                        [isDefault] => 1
                        [qty] => 1
                    )

            )

    )
)

Now I add some logic if some one add again that menu in add to cart in short he/she adds plus 1 qty to that menu here it is my code,(code is not completed yet but now I am focused on just to update qty)

            foreach($session->cartSession as $sessionKey=>$sessionVal)
            {

                        foreach($sessionVal['menuArray'] as $sessionMenuKey=>$sessionMenuVal)
                        {

                            if($sessionMenuKey == $post_data['menuId'])
                            {
                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
                                //$qty = $session->cartSession[$restaurantDetail['restId']]['menuArray'][$sessionMenuKey];
                                $sessionMenuVal['qty'] =  $sessionMenuVal['qty']+1;

                                echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>"; 

                            }

                         }
            }

What I am missing I want to updatre session qty.

every time I press add it shows 1 qty then I add plus 1 qty to it then it show 2 after then it again shows 1 qty :(.

Upvotes: 0

Views: 286

Answers (1)

Sam
Sam

Reputation: 16445

You're never writing back into the Session...

If i understand your code correctly, the following should work. Check the correct Array-Nesting of my code in case it doesn't work out of the box.

// Somewhere on top, use this for your loops
$cartSession = $session->offsetGet('cartSession'); 

// Inside your matched loop
$currentQty = $sessionMenuVal['qty'];
$cartSession[$sessionKey][$sessionMenuKey]['qty'] = ++$currentQty;

// After your loops at the end
$session->offsetSet('cartSession', $cartSession);

Upvotes: 2

Related Questions