Hey Juan,
Here is post that might help you: https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2014/11/remove-expired-carts/
And here is a screenshot of the job that you can just replicate and make small tweaks as suggested by Quan in the post above. Also, SQL query is not recommended.
Hi Juan,
I have created a scheduled job for you to delete the active carts.
[ScheduledPlugIn(
DisplayName = "Scheduled Delete Active Carts Job",
Description = "Delete all carts",
GUID = "84EBDA48-A71C-4BF9-8B39-0A0B3525EB0E",
Restartable = true,
SortIndex = -1000)]
public class ScheduledDeleteActiveCartsJab : ScheduledJobBase
{
private bool _stopSignaled;
private readonly IOrderSearchService _orderSearchService;
private readonly IOrderRepository _orderRepository;
public ScheduledDeleteActiveCartsJab( IOrderRepository orderRepository, IOrderSearchService orderSearchService)
{
_orderRepository = orderRepository;
_orderSearchService = orderSearchService;
IsStoppable = true;
}
/// <summary>
/// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
/// </summary>
public override void Stop()
{
_stopSignaled = true;
}
/// <summary>
/// Called when a scheduled job executes
/// </summary>
public override string Execute()
{
OnStatusChanged($"Starting execution of {this.GetType()}");
_stopSignaled = this.DeleteActiveCarts();
return $"All cart deleted successfully !!";
}
private bool DeleteActiveCarts()
{
var orderSearchFilter = new OrderSearchFilter();
var carts= _orderSearchService.FindCarts(orderSearchFilter);
foreach (var cart in carts.Orders)
{
_orderRepository.Delete(cart.OrderLink);
}
this.OnStatusChanged($"Total {carts.TotalRecords} carts deleted successfully !!");
return true;
}
}
Update code and used IOrderSearchService instead of ICartSearchService. Thanks Quan!
It's important to load carts by batch. If you have hundred of thousands of carts or even more, loading them in one go is to probably not a good idea. You can do batch loading with a do-while loop, and use RecordsToRetrieve to control how many carts you want to load in one batch.
Also ICartSearchService is an internal interface which should be avoided
I wrote about that here https://vimvq1987.com/iterate-through-all-carts-orders/
Hi
Is there any way to remove all current carts programmatically? Not using direct SQL querys
Maybe I can use the "remove old carts" job...