简体   繁体   中英

Clear Shopping Basket after set time of user inactivity

This is more of a query rather than problem.

I am trying to learn C#/MVC by building an online store with shopping cart. So far the cart does the basics; now I want to make it a bit more advanced.

I want to add the ability to clear out the cart if a user is inactive for a set time period (not actively using the site).

My thought is that this needs to be handled server side as the user could not even have their browser open and that the basket class would need to publish an event ('BasketTimeout'). I can then have a subscriber class which on receiving the event, runs the clear basket method (taking the basket ID as a parameter).

I don't want to chase a rabbit down the rabbit hole so would be grateful if someone could either point out the flaws in the above approach/suggest a cleaner approach.

Thank you for any help in advanced.

Wherever you are storing cart content, you can store here something like basket.lastActivity=now(); and of course, checking, if last activity was more than basket.lifeTime:

//this should be run on every user activity
if(basket.lastActivity + basket.lifeTime < now())
{
 basket.clear();
}
lastActivity = now();

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