LaravelShoppingcart icon indicating copy to clipboard operation
LaravelShoppingcart copied to clipboard

How to guests continue with same cart when they login

Open CODE-AXION opened this issue 2 years ago • 3 comments

i want to make a shopping cart where if guest user adds items to cart and when he logs in all the guest cart items should be shifted from session to database ? how can i achieve that ?

CODE-AXION avatar Jun 04 '22 08:06 CODE-AXION

Cart is saved in the databse or session. You could save it before login (write to db) and then load it after login (delete & load to session).

bumbummen99 avatar Jun 04 '22 09:06 bumbummen99

Cart is saved in the databse or session. You could save it before login (write to db) and then load it after login (delete & load to session).

But how can I store the session to the database , because there might be multiple session for multiple users ? And the session id will also get changed after login ? I am very confused idk how to do it properly 😞.

CODE-AXION avatar Jun 04 '22 10:06 CODE-AXION

Cart is saved in the databse or session. You could save it before login (write to db) and then load it after login (delete & load to session).

But how can I store the session to the database , because there might be multiple session for multiple users ? And the session id will also get changed after login ? I am very confused idk how to do it properly 😞.

No you dont save the session to the database. The cart can be either in the session or the database but not booth. A loaded cart is not on the database. https://github.com/Crinsane/LaravelShoppingcart/blob/f460ab7312cce32cb428cf39a500a2d16600b1d6/src/Cart.php#L399 Just store the cart with a tmp identifier and load it after the session is renewed.

bumbummen99 avatar Jun 04 '22 10:06 bumbummen99