Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Just the add cart line method from the cart manager
/// <summary>
/// Adds the line item to cart.
/// </summary>
/// <param name="storefront">The storefront.</param>
/// <param name="visitorContext">The visitor context.</param>
/// <param name="inputModelList">The input model.</param>
/// <returns>
/// The manager response where the result is retuned indicating the success or failure of the operation.
/// </returns>
public virtual ManagerResponse<CartResult, bool> AddLineItemsToCart([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, IEnumerable<AddCartLineInputModel> inputModelList)
{
Assert.ArgumentNotNull(inputModelList, "inputModelList");
var cartResult = this.LoadCartByName(storefront.ShopName, storefront.DefaultCartName, visitorContext.UserId, false);
if (!cartResult.Success || cartResult.Cart == null)
{
var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.CartNotFoundError);
cartResult.SystemMessages.Add(new SystemMessage { Message = message });
return new ManagerResponse<CartResult, bool>(cartResult, cartResult.Success);
}
var lines = new List<CartLine>();
foreach (var inputModel in inputModelList)
{
Assert.ArgumentNotNullOrEmpty(inputModel.ProductId, "inputModel.ProductId");
Assert.ArgumentNotNullOrEmpty(inputModel.CatalogName, "inputModel.CatalogName");
Assert.ArgumentNotNull(inputModel.Quantity, "inputModel.Quantity");
var quantity = (uint)inputModel.Quantity;
//// Special handling for a Gift Card
if (inputModel.ProductId.Equals(storefront.GiftCardProductId, StringComparison.OrdinalIgnoreCase))
{
inputModel.VariantId = inputModel.GiftCardAmount.Value.ToString("000", CultureInfo.InvariantCulture);
}
var cartLine = new CommerceCartLine(inputModel.CatalogName, inputModel.ProductId, inputModel.VariantId == "-1" ? null : inputModel.VariantId, quantity);
cartLine.Properties["ProductUrl"] = inputModel.ProductUrl;
cartLine.Properties["ImageUrl"] = inputModel.ImageUrl;
//// UpdateStockInformation(storefront, visitorContext, cartLine, inputModel.CatalogName);
lines.Add(cartLine);
}
var cartCache = CommerceTypeLoader.CreateInstance<CartCacheHelper>();
cartCache.InvalidateCartCache(visitorContext.GetCustomerId());
var cart = cartResult.Cart as CommerceCart;
var addLinesRequest = new AddCartLinesRequest(cart, lines);
addLinesRequest.RefreshCart(true);
// Set the custom properties here, get them from a cookie or any other mechanism
addLinesRequest.Properties["applyVIPStatus"] = true;
var addLinesResult = this.CartServiceProvider.AddCartLines(addLinesRequest);
if (addLinesResult.Success && addLinesResult.Cart != null)
{
cartCache.AddCartToCache(addLinesResult.Cart as CommerceCart);
}
this.AddBasketErrorsToResult(addLinesResult.Cart as CommerceCart, addLinesResult);
Helpers.LogSystemMessages(addLinesResult.SystemMessages, addLinesResult);
return new ManagerResponse<CartResult, bool>(addLinesResult, addLinesResult.Success);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment