Ad

OctoberCMS Update Session Value Not Working

- 1 answer

I am using OctoberCMS sessions https://octobercms.com/docs/services/session and its working fine for me but I have one requirement.

I am trying to update my session value by finding a session array id first and then updating a value out of the array of that session .. But its not working for me .. Here is what I am doing ..

My Html file

<a href="javascript:void(0)" data-request="{{ __SELF__ }}::onFramesSessionCreateOrUpdate" data-request-data="'productType':'1','productPlan':'2','productQty':'3'" data-request-update="cardlist: '#cardlist'">FrameS Add / Update Presenter</a>

My Php code

public function onFramesSessionCreateOrUpdate(){
        $productType = post('productType');
        $productPlan = post('productPlan');
        $productQty = post('productQty');

        // First we are checking if this product plan is already exists or not 
        // If already exists, then we need to update the session, else we need to add as new session 

        if (\Session::has('addedToCart.frames'))
        {
            $sessionData = \Session::get('addedToCart.frames');
            if(!empty($sessionData))
            {
                foreach (\Session::get('addedToCart.frames') as $key => $value) 
                {
                    if ($value['productPlan'] === $productPlan)
                    {
                        \Session::push('addedToCart.frames.'.$key . '.productQty.', $productQty);
                        break;
                        
                    }
                }
            }
            else{
                $sessionId = \Str::random(9);
                $array = array(
                    'id' => $sessionId,
                    'productType'=>$productType,
                    'productPlan'  => $productPlan,
                    'productQty' => $productQty
                );
                \Session::push('addedToCart.frames' , $array);
                
            }
            
        }
        else{
            
            $sessionId = \Str::random(9);
            $array = array(
                'id' => $sessionId,
                'productType'=>$productType,
                'productPlan'  => $productPlan,
                'productQty' => $productQty
            );
            \Session::push('addedToCart.frames' , $array);
        }
        
       
    }

Here in my html file I am calling a function onFramesSessionCreateOrUpdate on click and passing my parameters productType, productPlan and productQty and Inside onFramesSessionCreateOrUpdate function, First I am checking addedToCart.frames has a if ($value['productPlan'] === $productPlan).

If it has found I want to update only quantity of that and not removing a session at all ..

\Session::push('addedToCart.frames.'.$key . '.productQty.', $productQty); But this code doesnt work for me ..

If I do print_r following

echo '<pre>';
print_r($sessionData);
exit; 

My array looks like this

Array
(
    [0] => Array
        (
            [id] => SRgV5dJIC
            [productType] => 1
            [productPlan] => 2
            [productQty] => Array
                (
                    [] => Array
                        (
                            [0] => 3
                            [1] => 3
                        )

                )

        )

)

I have also tried below code

$productQty['productQty'] = $productQty;
\Session::push('addedToCart.frames.'.$key , $productQty);

But then the error saying legal string offset 'productQty'" on line 456 of D:\*\*\*\plugins\technobrave\loginplugin\components\LoginRegister.php

Can someone guide me please how can I just update the session .. I dont want to remove this session using Session::forget('key'); .. I just want to update the session values by the id I found.

Ad

Answer

You try to push session again so it is trying to add value in array instead of updating existing value.

You need to use \Session::put() method to override/replace value

This code will solve your issue

\Session::put('addedToCart.frames.' . $key . '.productQty', $productQty);

if any doubt please comment.

Ad
source: stackoverflow.com
Ad