DEV Community

wolfiton
wolfiton

Posted on

Shopping cart state saved in database or cookie for guests?

Hi, everyone

How would you save the shopping cart of a guest user that has left the page and wants to come back later to finish the checkout? (a database with an auto-generated UUID or create a profile with nulls for username and password and update once they create an account)

Avoid using local storage because if they switch to another device all their cart will be empty but it will contain items on the device they used(EX: has 3 items on PC but 0 on mobile for the same user).

Even though these are not very sensitive information like a user's credit card or a user's address some guest shoppers may feel like their privacy is invaded by such features. To save their cart to a database, but at the same time, they would feel that your website is lacking if they come later and find an empty cart.

Also leaving the e-commerce site may come down to some problems they didn't expect like, an electrical problem or an urgent problem came up.

How are you dealing with these dilemmas?(problems)

Thanks for any opinions and discussions on this subject

Top comments (8)

Collapse
 
chrispardy profile image
chris-pardy

Generally the best approach is to store a session token in session storage, or a cookie. Then keep a persistent cart in your database keyed off that session. Depending on what you are selling people may not care about cart persistence, or being able to start the shipping experience on one device, but keeping the data in your database let's you understand what people are interested in even if they don't checkout. It also let's you retarget people if you can capture email / other info, for instance when you have a sale on an item that was in their cart.

Collapse
 
themobiledev profile image
Chris McKay

This is how I do it. There's no identifying information as I don't capture any personal data at that point. Their shopping cart data is cleared if they make a purchase or if the item has been in their cart for more than 30 days (I run a software shop, so if they haven't purchased by then, I figure they're not going to purchase anything). Email and user data is captured when they buy a license, at which point the information is moved out of the shopping cart and into the license service.

Collapse
 
wolfiton profile image
wolfiton

Thanks, @chrismckay for sharing your experience and talking about in detail how you use this method to manage your software shop.

I really appreciated your thorough(detailed) response to my questions.

Collapse
 
wolfiton profile image
wolfiton • Edited

Thanks, @chris-pardy for sharing your solution and explanation of why you would choose this road to solve the problem, but I have a question:

How would you get the user info if they don't have an account and also wouldn't this be considered invasive(breach on their trust and privacy) to add the user's interest to a database without their knowledge? (the product they have put in the cart)
Also for me, GDPR is a must because I live in Europe.

Collapse
 
chrispardy profile image
chris-pardy

How you capture email or other info is ultimately a question of what your site offers and how far you or your local laws are willing to go what it comes to sophisticated online tracking. I'm not an expert on GDPR but I believe if a user signed up for a newsletter (email only, no login) you could still use their session data to retarget them for an abbandoned cart. Using the cart data for internal analytics would be possible even without personal information, and I don't think violates the trust of your users especially if that data is anonymous.

I would also say that while building a shopping cart is pretty straightforward there are some gotchas, GDPR amongst them. You're almost always going to be better off going with an ecommerce platform than rolling your own.

Thread Thread
 
wolfiton profile image
wolfiton

I have some doubts regarding this practice.

Thanks for explaining a lot more about this method and also for offering some examples of how this can be implemented, I really appreciate it.

Thanks @chris-pardy

Collapse
 
andmoredev profile image
Andres Moreno

What I have done is create a “session token” that I put in local storage. And that is what I use to save their items.
If they do not have an account I don’t expect them to have the enhanced cart experience for multiple devices.

Collapse
 
wolfiton profile image
wolfiton

Thanks, @andresmoreno for sharing your experience and your solution to this problem.