wojodesign / simplecart-js

A simple javascript shopping cart that easily integrates with your current website.
simplecartjs.org
1.79k stars 491 forks source link

Cart timeout functionality #451

Open russellfinlay opened 10 years ago

russellfinlay commented 10 years ago

I've attached a snippet of code to timeout (and delete) a cart after 15 minutes of user inactivity. Just add the code within your <script>...</script> tag.

Simply put, it sets a time 20 mins from now in the localStorage on each new page refresh. If the timeout variable is before the current time, it clears the cart.

Feel free to use at your will.

Note: The code only looks for page loads to indicate activity. To account for cart activity (adding and deleting items) I would suggest adding an event binding to execute this code.

if(localStorage.simpleCart_timeout){
  var timeout = new Date(localStorage.simpleCart_timeout);

  var now = new Date();
  if(now > timeout) {
    simpleCart.empty();
  }

}

var timeout = new Date();
timeout.setMinutes(timeout.getMinutes() + 15);
localStorage.simpleCart_timeout = timeout;
iwarner commented 10 years ago

Why would you want the cart to be deleted? Is this not again all UX in terms of selling products?

drivebass commented 10 years ago

@russellfinlay Nice one! I guess since the cart works with cookies it's pretty useful. In case the user has already added an item with a lower price and the admin decides to raise the price, the user will buy the item with the lower price if the item is still in the cart. Right? so isn't this a safe solution @iwarner ?