laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

Update Cart(sessionId)

Open zlatangoralija opened this issue 5 years ago • 8 comments

I'm currently in need to change the sessionId of a cart from guest user to authenticated user. Reading the documentation, I wasn't able to find a specific solution. So my question is, is it possible to change the Cart sessionId while keeping the cart contents, something like this:

$cart = Cart::session(oldSessionId); //Retrieve old session key
$cart->updateSessionId($newSessionId) //Update it with new session key

Thanks in advance.

zlatangoralija avatar Apr 16 '20 11:04 zlatangoralija

I'm currently in need to change the sessionId of a cart from guest user to authenticated user. why @zlatangoralija ? any specific needs for this, since you can keep the session id

frezno avatar Apr 25 '20 14:04 frezno

I need this because guest user can put items in cart. When he goes to the checkout page, there is an option to create an account. Now, when user creates account, I want to automatically log in that user and update cart to refer to that user account, instead of random session id I generated when the user visited the website. I hope that I gave you a good explanation

zlatangoralija avatar Apr 27 '20 11:04 zlatangoralija

ok, i see, @zlatangoralija , but i still don't understand, why you want to change the session id. Why don't you just keep the current session id? No need to change it. eg in my shop the guest can add to the cart, then, when checking out, log in (or register) the cart stays alive and i never touch the session id

frezno avatar Apr 27 '20 21:04 frezno

That's a good point. But at the start, I wanted to separate cart session completely by having different logic to initiate cart depending on if user is logged in or not, because I wanted to keep cart alive even if the user logs out. If I initiated cart with random session id every time, I wouldn't have an option to refer that cart to user->id, hence it wouldn't stay alive on logout.

zlatangoralija avatar Apr 28 '20 09:04 zlatangoralija

i understand, @zlatangoralija , but a session has a certain lifetime. For a logged in user i'd store the cart to the database as soon as he is logging in. That way he will always have access to his cart, even if he logged out and is logging in, let's say 2 weeks later. And if you want to, you can set a date on not checked out carts when they will be deleted.

frezno avatar Apr 28 '20 20:04 frezno

I have the same problem! Have any solution?

Shaykhnazar avatar Oct 19 '20 05:10 Shaykhnazar

Or not?

Shaykhnazar avatar Oct 19 '20 05:10 Shaykhnazar

I didn't find a solution I was looking for, but I managed to do this via sessions, where I would set some unique cart ID on page visit, and preserve it until user closes the page completely. This way, event if the user logs in, his cart would be preserved. Might not be the best solution, but it worked.

zlatangoralija avatar Oct 20 '20 07:10 zlatangoralija