Cancel Session in SilverStripe

I am creating a fairly simple online store in SilverStripe. I am writing a function to remove an item from the recycle bin ( order in my case).

My setup:

My endpoint returns JSON in a view for use in ajax.

 public function remove() { // Get existing order from SESSION $sessionOrder = Session::get('order'); // Get the product id from POST $productId = $_POST['product']; // Remove the product from order object unset($sessionOrder[$productId]); // Set the order session value to the updated order Session::set('order', $sessionOrder); // Save the session (don't think this is needed, but thought I would try) Session::save(); // Return object to view return json_encode(Session::get('order')); } 

My problem:

When I send data to this route, the product is deleted, but only temporarily, then called the next time, the previous item is returned.

Example:

Property order:

 { product-1: { name: 'Product One' }, product-2: { name: 'Product Two' } } 

When I send a message to remove product-1 , I get the following:

 { product-2: { name: 'Product Two' } } 

Which seems to have worked, but then I will try to remove product-2 and get the following:

 { product-1: { name: 'Product One' } } 

SON AB is back! When I remove the entire basket, it still contains both.

How do I get order to stick?

+5
source share
1 answer

Your expectation is true, and it should work with the code you wrote. However, the session data management method does not work well with deleted data, since it is not considered a state change. Only existing editable data is considered as such. See Session :: recursivelyApply () for more information. The only way I know is (unfortunately) to underline textmanipulate $ _SESSION just before setting a new value for 'order'

 public function remove() { // Get existing order from SESSION $sessionOrder = Session::get('order'); // Get the product id from POST $productId = $_POST['product']; // Remove the product from order object unset($sessionOrder[$productId]); if (isset($_SESSION['order'])){ unset($_SESSION['order']); } // Set the order session value to the updated order Session::set('order', $sessionOrder); // Return object to view return json_encode(Session::get('order')); } 
+3
source

All Articles