Пример #1
0
        public void AddProduct(Guid productId, int quantity)
        {
            if (_status != Enums.ShoppingCartStatus.ShoppingCart)
            {
                throw new Exception("Shopping cart checkedout. Can not add product");
            }
            if (quantity <= 0)
            {
                throw new Exception("You must input Quantity of product");
            }
            var     id = Guid.Parse(Id);
            Product product;

            using (var db = new CoreEcommerceDbContext())
            {
                product = db.Products.SingleOrDefault(i => i.Id == productId);
            }
            if (product == null)
            {
                throw new Exception("Your selected product was not exist");
            }
            long total             = 0;
            int  tempQuantity      = 0;
            var  shoppingCartItems = _items.Where(i => i.ProductId == productId).ToList();

            if (shoppingCartItems.Count > 0)
            {
                total        = shoppingCartItems.Sum(i => i.Quantity * i.UnitPrice);
                tempQuantity = shoppingCartItems.Sum(i => i.Quantity);
            }

            tempQuantity = tempQuantity + quantity;

            var  pp = ProductPromotionServices.CalculateDiscount(productId, tempQuantity);
            long productDiscount    = 0;
            Guid productPromotionId = Guid.Empty;

            if (pp != null)
            {
                productDiscount    = pp.DiscountValue;
                productPromotionId = pp.Id;
            }
            else
            {
                productDiscount = 0;
            }

            total = total + (quantity * product.Price) - productDiscount;

            ApplyChange(new ShoppingCartAddedProduct(id, productId, tempQuantity, product.Price, total, DateTime.Now));
            ApplyChange(new ShoppingCartItemAppliedProductPromotion(id, productId, productPromotionId, quantity, productDiscount));
        }
Пример #2
0
        private static void PreCalculateShoppingCart(Guid shoppingCartId, Guid productId, int productQuantity)
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext <SystemNotificationHub>();
            var msg        = string.Empty;

            var pp         = ProductPromotionServices.CalculateDiscount(productId, productQuantity);
            var ooDiscount = OrderPromotionServices.CalculateForDiscount(shoppingCartId);
            var ooShipping = OrderPromotionServices.CalculateForShipping(shoppingCartId);

            using (var db = new CoreEcommerceDbContext())
            {
                if (pp != null)
                {
                    msg += db.ContentLanguages.GetValue(pp.Id, "Description");
                }
                if (ooDiscount != null)
                {
                    msg += "<br>" + db.ContentLanguages.GetValue(ooDiscount.Id, "Description");
                }
                if (ooShipping != null)
                {
                    msg += "<br>" + db.ContentLanguages.GetValue(ooShipping.Id, "Description");
                }
            }

            if (!string.IsNullOrEmpty(msg))
            {
                hubContext.Clients.All.shoppingCartMessage(new NotificationMessage()
                {
                    DataType = "ShoppingCart",
                    DataJson = JsonConvert.SerializeObject(new
                    {
                        Id         = shoppingCartId,
                        ProductId  = productId,
                        ActionType = "PreCalculateShoppingCart",
                        Message    = msg
                    })
                });
            }

            hubContext.Clients.All.shoppingCartMessage(new NotificationMessage()
            {
                DataType = "ShoppingCart",
                DataJson = JsonConvert.SerializeObject(new
                {
                    Id         = shoppingCartId,
                    ActionType = "RefreshShoppingCart",
                    Message    = msg
                })
            });
        }