Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Remove item from session arry does not work using Session class


Reply


3 Posts   1738 Views

Avatar
Martijn

Community Member, 271 Posts

16 August 2009 at 11:09am

So, I wanted to build my own simple SS webshop with a session shopping cart.

Getting quite far with adding and modifing quantity in the session cart.

But when I want to remove an item from the array SS keeps falling back on the old session vars. Using a regular $_SESSION does work....

The cart items array is like:

$item = array($productid => $quantity);


Example which won't work:

function remove($productid){
      $cart_items = Session::get('cart_items');
      unset ($cart_items[$productid]);
      Session::clear('cart_items'); //even this will cause SS Session to pick the old array with the rmoved item.
      Session::set('cart_items', $cart_items);   //this works when modifying quantity, the new array is used correct   
      if(!$this->isAjax()) Director::redirectBack();
   }


Example which do work:

function remove($productid){
      $cart_items = Session::get('cart_items');
      unset ($cart_items[$productid]);
      $_SESSION['cart_items'] = $cart_items;      
      if(!$this->isAjax()) Director::redirectBack();
   }


Is this a bug or am I doing something wrong?

Avatar
dayer

Community Member, 11 Posts

28 August 2010 at 1:49pm

Hi,
a year later, I have the same problem.

Avatar
dayer

Community Member, 11 Posts

1 September 2010 at 7:06am

Hi again,

thanks to Martijn yesterday I discovered that this was problem due to I didn't use serialize and unserialize.