You shouldn't be using the carthelper, you should be using the IOrderRepositroy for cart order manipulation https://world.episerver.com/documentation/developer-guides/commerce/orders/shopping-carts/.
I'd suggest adding all the items in using this way and running the validation after.
I recall that you shouldn't mix CartHelper and IOrderRepository, but can't remember the exact reason. If someone does, please inform.
But regardless, I think you can do it with cart helper as well, but you don't need to do 500 saves to the cart, I can definately understand that that would be costly. Can't you just move the AcceptChanges() to outside of the loop? So you build up your cart in memory and then just flush it to the database once?
Scott,
Thaks for the suggestion.
I have gone through the link and codes. Seems it is a different approach to handle the cart process. I could not get a clue to add bunch items to the cart by a single hit.
I have the "List<Variation>". Currently, we are looping the list and adding items with the help of below API.
CartHelper.AddEntry(entry, item.Quantity, false);
Is there any chance to add like List of entries at the single hit?
The IOrderRepository has a Save method that you call after manipulating the cart. So if you add all your items by looping then you can just call the save once which will save all the information in one hit
Yes Joel. We are also using the same approach. But its an major need to add bunch cart (500 items and more) for the business. Below are the snippet we are using.
private async Task<ActionStatus> AddBunchToCart(IEnumerable<LineItemLite> items)
{
var variations = variationService.GetVariations(items.Select(x => x.Code));
items.ToList()?.ForEach(item =>
{
AddPriceToVariation(item.Code, item.Price);
var entry = CatalogContext.Current.GetCatalogEntry(item.Code);
if (entry != null)
{
var lineItem = GetLineItem(item.Code);
if (lineItem == null)
{
CartHelper.AddEntry(entry, item.Quantity, false);
}
else
{
status.Success = false;
status.WarningMessage += "\n" + $"Can't find product with code {item.Code}";
}
}
});
CartHelper.Cart.AcceptChanges();
var message = ValidateCart();
status.WarningMessage += "\n" + message;
return status;
}
Also my suggestion of the IOrderRepository follows Episerver guidance, the CartHelper is Obselete code and may be removed at some point https://world.episerver.com/blogs/Quan-Mai/Dates/2018/1/carthelper-is-dead-long-live-iorderrepository-/
There are several reasons to not use CartHelper now. It does not allow mocking and hard to test. It relies on Entry object which is heavy to load. Entry and CartHelper are also obsoleted now so to speak.
The reason we don't recommend mixing CartHelper (or any concrete implementation ) with the new abstraction API is because that might lead to missing data. You should not load a cart with CartHelper and save it with IOrderRepository or vice versa.
As previously said, you should use the IOrderRepository APIs as it is more lightweight. Also you only need to save once not everytime. CartHelper.AddEntry saves the cart internally, so you might want to rewrite that part if switching to abstraction APIs is not an option. Carthelper belongs to Mediachase.Commerce.Website which is open sourced.
Thanks to all for your valuable suggestions. We will keep this as a note and proceed it.
Thanks again!!!
Hi All,
We have an implementation to adding the bulk items to the cart. Unfortunately, the increasing number would cost a poor performance. Can we have an API for adding bunch items to the cart in a single API hit?
For instance,
items count can be 500 and more...
foreach // items{
var entry = CatalogContext.Current.GetCatalogEntry(item.Code);
CartHelper.AddEntry(entry, item.Quantity, false);
CartHelper.Cart.AcceptChanges();
}
Expecting solution:
var entries = CatalogContext.Current.GetCatalogEntry(items);
CartHelper.AddEntrys(entries,false);
CartHelper.Cart.AcceptChanges();
Thanks in advance...