November Happy Hour will be moved to Thursday December 5th.

How can I get all carts?

Vote:
 

I would like to get all saved shopping carts from 7 days back (for all customers)? Is there some way to do this with the API?

I am using Commerce 11.

#193808
Jun 05, 2018 14:02
Vote:
 

https://world.episerver.com/documentation/developer-guides/commerce/orders/Searching-for-orders/

You can set it to shopping carts and build ypur own query. Note that you should check how the resulting query ends up so it's as performant as you want it to be.

#193810
Jun 05, 2018 14:27
Vote:
 

If you are using the (old) concrete cart, then as Joel said you can use the order search API to find the carts. 

                int startRowIndex = 0;
                OrderSearchParameters parameters = new OrderSearchParameters
                {
                    SqlMetaWhereClause = $"Meta.Modified >  GETDATE() - 7",
                };

                OrderSearchOptions options = new OrderSearchOptions {Namespace = "Mediachase.Commerce.Orders"};
                options.Classes.Add(OrderContext.ShoppingCartClassType);
                options.RecordsToRetrieve = 50;

                int totalRecords;
                do
                {
                    options.StartingRecord = startRowIndex;
                    Cart[] carts = OrderContext.Current.FindCarts(parameters, options, out totalRecords);
                    if (carts != null)
                    {
                        foreach (Cart cart in carts)
                        {
                                //Do stuffs
                        }

                        totalRecords = carts.Count();
                    }
                }
                while (totalRecords > 0);

If you are using serializable cart then things are a bit more complicated as you would have to use an internal class for that 

            var cartFilter = new CartFilter()
            {
                ModifiedFrom = DateTime.UtcNow.AddDays(-7),
                StartingRecord = 0,
                RecordsToRetrieve = 100
            };

            do
            {
                var carts= _serializableCartDB.Service.FindCarts(cartFilter));

                foreach (var cart in carts)
                {
                }

                totalRecords = carts.Count();
            }
            while (totalRecords > 0);
#193811
Edited, Jun 05, 2018 14:42
Vote:
 

@joel:

Do you know how such a query would look (retrieving all carts not older than 7 days)?

OrderSearchOptions searchOptions = new OrderSearchOptions();
searchOptions.CacheResults = false;
searchOptions.StartingRecord = 0;
searchOptions.RecordsToRetrieve = 10000;
searchOptions.Namespace = "Mediachase.Commerce.Orders";

OrderSearchParameters parameters = new OrderSearchParameters(); searchOptions.Classes.Add("ShoppingCart"); parameters.SqlMetaWhereClause = ""; parameters.SqlWhereClause = ""; Cart[] cartCollection = OrderContext.Current.FindCarts(parameters, searchOptions);

Thanks!

#193812
Edited, Jun 05, 2018 14:43
Vote:
 

@Quan: seems to work, thanks :-)

Filter should be like this, though:

 var cartFilter = new CartFilter()
{
ModifiedFrom = DateTime.UtcNow.AddDays(-7),
ModifiedTo = DateTime.UtcNow,
StartingRecord = 0,
RecordsToRetrieve = 100
};
#193814
Edited, Jun 05, 2018 14:59
Vote:
 

True, I did not realize the condition was for carts OLDER than 7 days. I updated the post now.

#193820
Jun 05, 2018 15:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.