public async Task <ActionResult> Add(string id, int quantity) { EnsureThatCartExist(); using (await AsyncLock.GetLockByKey(GetAsyncLockCartKey(_workContext.CurrentCart)).LockAsync()) { var product = (await _catalogService.GetProductsAsync(new string[] { id }, Model.Catalog.ItemResponseGroup.ItemLarge)).FirstOrDefault(); if (product != null) { await _cartBuilder.AddItemAsync(product, quantity); await _cartBuilder.SaveAsync(); } } return(StoreFrontRedirect("~/cart")); }
public async Task <ActionResult> Add(string id, int quantity) { await _cartBuilder.GetOrCreateNewTransientCartAsync(WorkContext.CurrentStore, WorkContext.CurrentCustomer, WorkContext.CurrentLanguage, WorkContext.CurrentCurrency); var product = (await _catalogService.GetProductsAsync(new string[] { id }, Model.Catalog.ItemResponseGroup.ItemLarge)).FirstOrDefault(); if (product != null) { await _cartBuilder.AddItemAsync(product, quantity); await _cartBuilder.SaveAsync(); } return(StoreFrontRedirect("~/cart")); }
private async Task <string[]> TryAddItemsToCartAsync(BulkOrderItem[] bulkOrderItems) { var skus = bulkOrderItems.Select(i => i.Sku); var productSearchResult = await _catalogService.SearchProductsAsync(new ProductSearchCriteria { PageSize = skus.Count(), Terms = new[] { new Term { Name = "code", Value = string.Join(",", skus) } } }); foreach (var product in productSearchResult.Products) { var bulkOrderItem = bulkOrderItems.FirstOrDefault(i => i.Sku == product.Sku); if (bulkOrderItem != null) { await _cartBuilder.AddItemAsync(product, bulkOrderItem.Quantity); } } await _cartBuilder.SaveAsync(); var notFoundedSkus = bulkOrderItems.Select(x => x.Sku).Except(productSearchResult.Products.Select(x => x.Sku)) .Distinct().ToArray(); return(notFoundedSkus); }
private async Task <string[]> TryAddItemsToCartAsync(BulkOrderItem[] bulkOrderItems) { var skus = bulkOrderItems.Select(i => i.Sku).ToList(); //TODO: Need to replace from indexed search to special method GetProductByCodes when it will be presents in the catalog API var productSearchResult = await _catalogService.SearchProductsAsync(new ProductSearchCriteria { PageSize = skus.Count(), ResponseGroup = ItemResponseGroup.Variations | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Inventory | ItemResponseGroup.ItemProperties, Terms = new[] { new Term { Name = "code", Value = string.Join(",", skus) } } }); //Because product stores in index all codes of it variations and the catalog search returns only main product we need to this concat with variations var foundSkusProductsWithVariationsMap = productSearchResult.Products.Concat(productSearchResult.Products.SelectMany(x => x.Variations)) .ToDictionary(x => x.Sku, StringComparer.OrdinalIgnoreCase); var addedSkus = new List <string>(); foreach (var bulkOrderItem in bulkOrderItems) { if (foundSkusProductsWithVariationsMap.TryGetValue(bulkOrderItem.Sku, out var product)) { if (await _cartBuilder.AddItemAsync(product, bulkOrderItem.Quantity)) { addedSkus.Add(product.Sku); } } } await _cartBuilder.SaveAsync(); return(skus.Except(addedSkus).ToArray()); }
public async Task <ActionResult> AddItemJson(string id, int quantity = 1) { await _cartBuilder.GetOrCreateNewTransientCartAsync(WorkContext.CurrentStore, WorkContext.CurrentCustomer, WorkContext.CurrentLanguage, WorkContext.CurrentCurrency); var product = await _catalogService.GetProductAsync(id, Model.Catalog.ItemResponseGroup.ItemLarge); if (product != null) { await _cartBuilder.AddItemAsync(product, quantity); await _cartBuilder.SaveAsync(); } return(Json(null, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> AddItemJson(string id, int quantity = 1) { EnsureThatCartExist(); //Need lock to prevent concurrent access to same cart using (var lockObject = await AsyncLock.GetLockByKey(GetAsyncLockCartKey(id)).LockAsync()) { var products = await _catalogService.GetProductsAsync(new string[] { id }, Model.Catalog.ItemResponseGroup.ItemLarge); if (products != null && products.Any()) { await _cartBuilder.AddItemAsync(products.First(), quantity); await _cartBuilder.SaveAsync(); } } return(Json(null, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> AddItemToCart(string id, int quantity = 1) { EnsureThatCartExist(); //Need lock to prevent concurrent access to same cart using (await AsyncLock.GetLockByKey(GetAsyncLockCartKey(WorkContext.CurrentCart)).LockAsync()) { var products = await _catalogSearchService.GetProductsAsync(new[] { id }, Model.Catalog.ItemResponseGroup.ItemLarge); if (products != null && products.Any()) { await _cartBuilder.AddItemAsync(products.First(), quantity); await _cartBuilder.SaveAsync(); } } return(Json(new { _cartBuilder.Cart.ItemsCount })); }