Jump to content

Can't store an Array in a PW $session ?


suntrop
 Share

Recommended Posts

Hi all. Perhaps this is just my fault, but I can't get behind it :-(

When I store an array in a $session and want to read its contents – after a $session->redirect() – it is NULL by var_dump()

If I use the exact same code and just store a string or a Page ID the output is correct.

// file-1, product page
$last_add_to_cart['product'] = $selected_product;
$last_add_to_cart['quantity'] = $selected_product_quantity;
$session->last_add_to_cart = $last_add_to_cart;

$session->redirect('/warenkorb/');


// file-2, cart
var_dump($session->last_add_to_cart); // is  NULL

Strangely this works 

// file-1, product page
$last_add_to_cart['product'] = $selected_product->id;
#$last_add_to_cart['quantity'] = $selected_product_quantity;
$session->last_add_to_cart = $last_add_to_cart;

$session->redirect('/warenkorb/');


// file-2, cart
var_dump($session->last_add_to_cart); // is 1234

EDIT: When I var_dump the $session before the redirect everything is ok

Edited by suntrop
Added info at the end
Link to comment
Share on other sites

3 hours ago, suntrop said:

$last_add_to_cart['product'] = $selected_product;

I don't think you can store objects in $session. See this comment from Ryan for instance:

Storing an array (without objects) seems to work:

$my_array = [];
$my_array['animal'] = 'cat';
$my_array['colour'] = 'orange';
$session->my_array = $my_array;

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...