The simplest option could be to change the default cookie timeout (which is set for 1 year). If you set this to 360 it will mean the cart cookie expires after 6 hours of inactivity.
It would mean the order isn't removed though, it just looses it's link with the current session so a new one would be created.
If you wanted more control over this, then Huw's suggestion would be an altrnative route, setting up a background task and checking to see how long an order has been inactive for and if it exeeds your time limit, remove all items from the cart.
A background task won't have the concept of a "current order" as these are user specific (stored in a cookie). You'd really want to perform a search for all orders that have not been updated within your expiry window and then empty all of them.
public void ClearBaskets()
{
int vendrBasketExpiryTimeHours = _configuration.GetValue<int>("AppSettings:VendrBasketExpiryTimeHours");
var orders = _vendrApi.SearchOrders(o => o.CreatedBefore(DateTime.Now.AddHours(-vendrBasketExpiryTimeHours)), 1, int.MaxValue);
if (orders != null && orders.Items.Any())
{
foreach (var item in orders.Items.Where(i => !i.IsFinalized))
{
_unitOfWorkProvider.Execute(uow =>
{
// Convert the order into it's Writable form
var order = item.AsWritable(uow);
// Peform our write operation
foreach (var orderLine in order.OrderLines)
{
order.WithOrderLine(orderLine.Id).SetQuantity(0);
}
// Persist the changes to the database
_orderService.SaveOrder(order);
//_orderService.DeleteOrder(order);
// Close our transaction
uow.Complete();
});
}
}
}
should I reset the quantity to 0 for each orderline, or just delete the entire order?
I would put the finalized check as part of the search statement as you don't want to load ALL orders and then exclude the finalized, you might as well make it part of the same statement and just get back the results you want to loop over.
In terms of clearing the order lines, or deleting the order, it's up to you and whether you want to keep any customer data that may already be stored on the order.
I'd probably get all the order line ID's, then loop over them and call RemoveOrderLine(id) to remove them all (we could probably do with a method to remove all order lines)
Clear basket after timeout
What is the proper way to delete the current users basket after a set period of time? i.e we want the users cart/basket to only show for 6 hrs.
I can't speak specifically for Vendr, but we use a scheduled job to check for open baskets that have been sat around for more than X hours.
Hey Ian,
The simplest option could be to change the default cookie timeout (which is set for 1 year). If you set this to 360 it will mean the cart cookie expires after 6 hours of inactivity.
It would mean the order isn't removed though, it just looses it's link with the current session so a new one would be created.
If you wanted more control over this, then Huw's suggestion would be an altrnative route, setting up a background task and checking to see how long an order has been inactive for and if it exeeds your time limit, remove all items from the cart.
Hi Matt, thanks for that.
For the scheduled task, I was thinking of something like this:
does that look correct, have i used the correct session manager methods?
Hey Ian,
Hmm, not exactly.
A background task won't have the concept of a "current order" as these are user specific (stored in a cookie). You'd really want to perform a search for all orders that have not been updated within your expiry window and then empty all of them.
So something like this:
should I reset the quantity to 0 for each orderline, or just delete the entire order?
I would put the finalized check as part of the search statement as you don't want to load ALL orders and then exclude the finalized, you might as well make it part of the same statement and just get back the results you want to loop over.
In terms of clearing the order lines, or deleting the order, it's up to you and whether you want to keep any customer data that may already be stored on the order.
I'd probably get all the order line ID's, then loop over them and call
RemoveOrderLine(id)
to remove them all (we could probably do with a method to remove all order lines)is working on a reply...