简体   繁体   中英

storing a shopping cart when no user is logged in(playframework doubt)

I was browsing through posts about Play! framework Play! framework and came across some posts which discussed ecommerce .Since I am a beginner to both,I thought of doing an exercise.I wrote down some use cases and some requirements as below.I would like to hear your opinion about it,hoping it may broaden my technical wisdom.

Some requirements for shopping cart on a webpage:

1.User can add items to cart even without logging in to the site.

2.User need to login after he clicks on checkout link .

3.Itemdetails page will contain an addtocart button only if that item is not already in the cart.

4.Itemdetails page will contain a minicartview ,showing names of items in cart and total price.

I coded the ShoppingCart as below.It can be retrieved from db using it's User .

@Entity
class ShoppingCart{
...
   @OneToOne
   public User user;
}

Some possible scenarios I considered.

1.User is already logged in ,when he comes to the Itemdetails page.

This seems easy,I can retrieve the cart from db,using the logged in user.

User user = User.find("byEmail", Security.connected()).first();
cart = ShoppingCart.find("byUser",user);
...

2.No user logged in at present.

This is what bothers me.How to handle this?How can I show the minicartview here on Itemdetails page?Where should I store the cart?Cache is volatile and cannot be trusted .How else should I store the cart?I cannot store in db until a user logs in.

If someone can make things clear about this scenario,it may help me a lot..

The way you are modeling this a user can have only one cart. You should create a cart for each session instead. Use session id as primary key for the cart entity and do not associate with User.

also see: advice on implementing a shopping cart using playframework

You have a few options. You could

  1. Store the shopping cart items in your session (as it is a cookie, try to limit the amount you store). When the user logs in, you can then merge your session shopping cart items with their logged in user items. As with sites, such as Amazon, it would make sense to try to store a long-live session on the user's computer to remember the id of the logged in user for later usage.

  2. Store the data in a database against a temporary username. This would act much like the session, but you would have to have a regular job that would clear down the temporary users database table if it is not converted to a full user.

Personally, I would go for option 1. It is fairly simple to implement, there is nothing personal in the cookie (as it is just IDs), and it is meant as a short term storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM