Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

How to guests continue with same cart when they login #670

Open CODE-AXION opened 2 years ago

CODE-AXION commented 2 years ago

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 ?

bumbummen99 commented 2 years ago

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).

CODE-AXION commented 2 years ago

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 😞.

bumbummen99 commented 2 years ago

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.