Removing all active carts

Vote:
 

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...

#247537
Jan 23, 2021 23:05
Vote:
 

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.

#247538
Jan 24, 2021 2:07
Vote:
 

 Hi Juan,

I have created a scheduled job for you to delete the active carts. 

  • Load all carts using IOrderSearchService and pass the filter option (OrderSearchFilter) in the FindCarts method if you want to load the carts for a specific market, customer, date range, etc.
  • Use IOrderRepository and delete the cart entry.
[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!

#247549
Edited, Jan 24, 2021 17:35
Vote:
 

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 

#247587
Jan 25, 2021 9:49
Sanjay Kumar - Jan 25, 2021 10:53
I have already suggested to use 'OrderSearchFilter' to load a specific date range carts instead of loading all carts at one time.
If ICartSearchService is an internal interface then which interface we used to load all customer carts?
Quan Mai - Jan 25, 2021 12:21
IOrderSearchService is the public one and it supports searching for ICart
Vote:
 
#247598
Jan 25, 2021 15:52
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.