2

Working on a E-commerce solution where I need to handle checkout based on anonymous customer and as of now I am not able to think properly how best this can be implemented.

Our ShoppingCart is being saved in database and and every update/ edit in ShoppingCart is being updated in database.

Now I need to take care about creating an anonymous customer and than assign this cart to that customer so that add to cart and well checkout can be associated with this customer.

Can anyone suggest me what can be the right way to go for this?

  1. Should I create one anonymous user in database and use it everytime a request for new customer (anonymous ) is being created.
  2. Place that user in current user session.
  3. Perform any operation on cart with respect to the current session
Umesh Awasthi
  • 377
  • 2
  • 5
  • 11
  • Cookies would be another option. A tiny bit more consistent than session (So closing browser and coming back later you still have that cart). – thorsten müller Nov 28 '13 at 13:05
  • @thorstenmüller: that is already there, we are using cookies to store cart id so that same can be fetched from the DB again. – Umesh Awasthi Nov 28 '13 at 14:16

1 Answers1

5

I think there are some challenges in your approach. For example, what's going to happen when you have two users shopping before they have signed in? From your current suggestion, it sounds like there could be problems with keeping the carts separate and making sure they can't see each other's cart. Or what happens when a previous anonymous session has ended and a new one comes in afterwards? Will they inadvertently receive the previous anonymous person's cart?

You don't need to associate the cart with anything except the session or a cookie until the user chooses to checkout. So I would throw out the first part of your suggestion with creating an anonymous user and assigning that to the session. It's okay to leave the user as null at this point in time.

For your second step, I would create a new cart and session instead of placing them in a current user session. The exception would be if you have something persistent, such as a cookie, to tie the anonymous user to a particular cart.


The key difference is changing your point of view on how you look at users, sessions, and carts.
Currently, I think you're seeing it as: a user has a session which may have a cart.
What I'm suggesting is you see it as: a session may have a cart and may also have a user.

Taking that approach will change up your object hierarchy and help you view things in a more flexible manner in supporting anonymous shopping sessions.

  • +1 That is really a good approach will try to see how I can go ahead with this.Currently my user is null and I am creating a new session and placing ShoppingCart in it. – Umesh Awasthi Nov 28 '13 at 14:06