Jump to:

22976 Posts in 11493 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Remove item from session arry does not work using Session class

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

Page: 1
Go to End
Author Topic: 1598 Views
  • Martijn
    Avatar
    Community Member
    271 Posts

    Remove item from session arry does not work using Session class Link to this post

    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?

  • dayer
    Avatar
    Community Member
    11 Posts

    Re: Remove item from session arry does not work using Session class Link to this post

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

  • dayer
    Avatar
    Community Member
    11 Posts

    Re: Remove item from session arry does not work using Session class Link to this post

    Hi again,

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

    1598 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.