Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anton123's avatar

How to store shopping carts belonging to unregistered users?

I'm currently working on an eCommerce website and I want to allow users to order products without registering to the site. The workflow is straight forward for authenticated users, I have database tables for the shopping cart, shopping cart items etc. but I'm not sure how should I handle guests? Since I can't access their ID, should I store the data in a session or what?

0 likes
5 replies
OussamaMater's avatar
Level 37

Best thing in this case is to use LocalStorage to save the products they want to buy, and once they created an account or logged in, you can check if there is any products in the LocalStorage to show, and you don't have to worry about it if they close the page or the browser, it has no expiration date.

Here is an article to get you started:

1 like
anton123's avatar

@OussamaMater doesn't the use of local storage prevent the use of incognito/private browsing? Would cookies be a better option?

OussamaMater's avatar

@anton123 I don't really think you have to worry about private browsing as it is built to clear up all the LocalStorage/Cookies and everything else once its close, so even the user who's using the private browser does not expect you to save his wishlist or whatever it is, because it's not doable. (you can have a look at some big ecommerce sites, if you use private browsing and save something to the cart, it won't be there once you close your brower).

PS: LocalStorage will work normally like you expect in normal browsing as long as the browser is open, so if they user added something, and later on decided to create an account and logged in it will be there.

anton123's avatar

@OussamaMater oh, my worry was that I won't be able to use local storage at all in incognito browsers, but I now know that is not the case. Thank you! I will definetly check it out :)

Please or to participate in this conversation.