private void UpdateShoppingCart(IEnumerable <UpdateShoppingCartItemViewModel> items)
        {
            _shoppingCart.Clear();

            if (items == null)
            {
                return;
            }

            var minimumOrderQuantities = GetMinimumOrderQuantities(items);

            _shoppingCart.AddRange(
                items
                .Where(item => !item.IsRemoved)
                .Select(item => new ShoppingCartItem(
                            item.ProductId,
                            item.Quantity <= 0 ? 0 : item.Quantity < minimumOrderQuantities[item.ProductId] ? minimumOrderQuantities[item.ProductId] : item.Quantity,
                            item.AttributeIdsToValues))
                );

            _shoppingCart.UpdateItems();

            _workflowManager.TriggerEvent("CartUpdated",
                                          _wca.GetContext().CurrentSite,
                                          () => new Dictionary <string, object> {
                { "Cart", _shoppingCart }
            });
        }
        private void UpdateShoppingCart(FormCollection form)
        {
            var formKeys = form.AllKeys;
            var items    = (from key in formKeys
                            where key.Length > 3
                            where key.Substring(0, 3) == "prd"
                            select new UpdateShoppingCartItemVM
            {
                ProductId = Convert.ToInt32(key.Substring(3)),
                Quantity = Convert.ToInt32(form[key]),
                Size = form["size" + key.Substring(3)]
            }).ToList();

            if (items.Count == 0)
            {
                return;
            }

            _shoppingCart.Clear();

            _shoppingCart.AddRange(items
                                   .Where(item => !item.IsRemoved)
                                   .Select(item => new ShoppingCartItem(item.ProductId, item.Size, item.Quantity < 0 ? 0 : item.Quantity))
                                   );

            _shoppingCart.UpdateItems();
        }
 private void UpdateShoppingCart(IEnumerable <UpdateShoppingCartItemViewModel> items)
 {
     _shoppingCart.Clear();
     if (items == null)
     {
         return;
     }
     _shoppingCart.AddRange(items
                            .Where(item => !item.IsRemoved)
                            .Select(item => new ShoppingCartItem(item.ProductId, item.Quantity < 0 ? 0 : item.Quantity)));
 }
 private static void FillCart(IShoppingCart cart)
 {
     cart.AddRange(OriginalQuantities
                   .Select(q => new ShoppingCartItem(q.Product.Id, q.Quantity, q.AttributeIdsToValues)));
 }
Exemplo n.º 5
0
 private static void FillCart(IShoppingCart cart)
 {
     cart.AddRange(OriginalQuantities.Reverse()
                   .Select(q => new ShoppingCartItem(q.Product.Id, q.Quantity)));
 }
        private void UpdateShoppingCart(IEnumerable <UpdateShoppingCartItemViewModel> items)
        {
            var oldItems = new List <ShoppingCartItem>();

            oldItems.AddRange(_shoppingCart.Items); //create a copy for analysis
            _shoppingCart.Clear();

            if (items == null)
            {
                //removed all items
                foreach (var handler in _cartLifeCycleEventHandlers)
                {
                    //we raise the ItemRemoved event for all the removed items.
                    //The ShoppingCartItem objects also contain the quantity of the variation
                    handler.Updated(new List <ShoppingCartItem>(0), oldItems);
                }
                return;
            }



            var minimumOrderQuantities = GetMinimumOrderQuantities(items);

            _shoppingCart.AddRange(
                items
                .Where(item => !item.IsRemoved)
                .Select(item => new ShoppingCartItem(
                            item.ProductId,
                            item.Quantity <= 0 ? 0 : item.Quantity < minimumOrderQuantities[item.ProductId] ? minimumOrderQuantities[item.ProductId] : item.Quantity,
                            item.AttributeIdsToValues))
                );

            _shoppingCart.UpdateItems();
            //analyze update to raise events
            var addedItems   = new List <ShoppingCartItem>();
            var removedItems = new List <ShoppingCartItem>();
            var newItems     = new List <ShoppingCartItem>();

            newItems.AddRange(_shoppingCart.Items);
            //we use a KeyValuePair because item.QUantity is not alowed to be negative
            var itemsInBoth = new List <KeyValuePair <int, ShoppingCartItem> >(); //quantity variation, item

            itemsInBoth.AddRange(
                newItems
                .Select(newSci => {
                var oldSci = oldItems
                             .FirstOrDefault(sci => ShoppingCartItem.ItemsAreEqual(sci, newSci)); //item was in cart already
                int quantityVariation = 0;
                if (oldSci != null)
                {
                    quantityVariation = newSci.Quantity - oldSci.Quantity;
                    oldSci.Quantity   = Math.Abs(quantityVariation);
                }
                return(new KeyValuePair <int, ShoppingCartItem>(quantityVariation, oldSci));
            })
                .Where(kvp => kvp.Value != null));
            foreach (var item in itemsInBoth.Where(kvp => kvp.Key != 0))
            {
                if (item.Key > 0)   //increase in quantity
                {
                    addedItems.Add(item.Value);
                }
                else if (item.Key < 0)     //decrease in quantity
                {
                    removedItems.Add(item.Value);
                }
                //if quantity has not changed, we do not raise an event for that item
            }
            addedItems.AddRange(
                newItems.Where(sci => !itemsInBoth.Any(isci => ShoppingCartItem.ItemsAreEqual(isci.Value, sci)))
                );
            removedItems.AddRange(
                oldItems.Where(sci => !itemsInBoth.Any(isci => ShoppingCartItem.ItemsAreEqual(isci.Value, sci)))
                );

            foreach (var handler in _cartLifeCycleEventHandlers)
            {
                handler.Updated(addedItems, removedItems);
            }
        }