Пример #1
0
        public async Task <ActionResult> AddItem(ShoppingCartLineUpdateModel line, string shoppingCartId = null)
        {
            ShoppingCartItem parsedLine = await _shoppingCartHelpers.ParseCartLine(line);

            if (parsedLine is null)
            {
                _notifier.Add(NotifyType.Error, H["Product with SKU {0} not found.", line.ProductSku]);
                return(RedirectToAction(nameof(Index), new { shoppingCartId }));
            }
            await _priceService.AddPrices(new[] { parsedLine });

            if (!parsedLine.Prices.Any())
            {
                _notifier.Add(NotifyType.Error, H["Can't add product {0} because it doesn't have a price.", line.ProductSku]);
                return(RedirectToAction(nameof(Index), new { shoppingCartId }));
            }
            IList <ShoppingCartItem> cart = await _shoppingCartPersistence.Retrieve(shoppingCartId);

            ShoppingCartItem existingItem = _shoppingCartHelpers.GetExistingItem(cart, parsedLine);

            if (existingItem != null)
            {
                int index = _shoppingCartHelpers.RemoveItem(cart, existingItem);
                cart.Insert(index, new ShoppingCartItem(existingItem.Quantity + line.Quantity, existingItem.ProductSku, existingItem.Attributes, existingItem.Prices));
            }
            else
            {
                cart.Add(parsedLine);
            }
            await _shoppingCartPersistence.Store(cart, shoppingCartId);

            return(RedirectToAction(nameof(Index), new { shoppingCartId }));
        }
        public async Task <ActionResult> AddItem(ShoppingCartLineUpdateModel line, string shoppingCartId = null)
        {
            ShoppingCartItem parsedLine = await _shoppingCartHelpers.ParseCartLine(line);

            if (parsedLine is null)
            {
                await _notifier.AddAsync(NotifyType.Error, H["Product with SKU {0} not found.", line.ProductSku]);

                return(RedirectToAction(nameof(Index), new { shoppingCartId }));
            }
            parsedLine = (await _priceService.AddPrices(new[] { parsedLine })).Single();
            if (!parsedLine.Prices.Any())
            {
                await _notifier.AddAsync(NotifyType.Error, H["Can't add product {0} because it doesn't have a price.", line.ProductSku]);

                return(RedirectToAction(nameof(Index), new { shoppingCartId }));
            }
            ShoppingCart cart = await _shoppingCartPersistence.Retrieve(shoppingCartId);

            cart.AddItem(parsedLine);
            await _shoppingCartPersistence.Store(cart, shoppingCartId);

            if (_workflowManager != null)
            {
                await _workflowManager.TriggerEventAsync(
                    nameof(ProductAddedToCartEvent),
                    new
                {
                    LineItem = parsedLine
                },
                    "ShoppingCart-" + _shoppingCartPersistence.GetUniqueCartId(shoppingCartId));
            }
            return(RedirectToAction(nameof(Index), new { shoppingCartId }));
        }
        public async Task <ActionResult> AddItem(ShoppingCartLineUpdateModel line, string shoppingCartId = null)
        {
            ShoppingCartItem parsedLine = await _shoppingCartHelpers.ParseCartLine(line);

            if (parsedLine is null)
            {
                throw new System.ArgumentException($"Product with SKU {line.ProductSku} not found.", nameof(line));
            }
            await _priceService.AddPrices(new[] { parsedLine });

            IList <ShoppingCartItem> cart = await _shoppingCartPersistence.Retrieve(shoppingCartId);

            ShoppingCartItem existingItem = _shoppingCartHelpers.GetExistingItem(cart, parsedLine);

            if (existingItem != null)
            {
                int index = _shoppingCartHelpers.RemoveItem(cart, existingItem);
                cart.Insert(index, new ShoppingCartItem(existingItem.Quantity + line.Quantity, existingItem.ProductSku, existingItem.Attributes, existingItem.Prices));
            }
            else
            {
                cart.Add(parsedLine);
            }
            await _shoppingCartPersistence.Store(cart, shoppingCartId);

            return(RedirectToAction(nameof(Index), new { shoppingCartId }));
        }