コード例 #1
0
        public void CalculateShipping(string address, double addressLatitude, double addressLongitude, Guid shippingMethodId)
        {
            if (_items == null || _items.Count == 0)
            {
                throw new Exception("No item to calculate");
            }

            if (string.IsNullOrEmpty(address) || shippingMethodId == Guid.Empty)
            {
                throw new Exception("address and shipping method required");
            }

            if (_isPreCalculated == false)
            {
                PreCalculate();
            }

            var  id               = Guid.Parse(Id);
            long shippingFee      = ShippingMethodServices.ShippingCost(id, shippingMethodId, address, addressLatitude, addressLongitude);
            var  orderPromotionId = Guid.Empty;
            var  orderPromo       = OrderPromotionServices.CalculateForShipping(id);

            if (orderPromo != null)
            {
                if (orderPromo.FreeShip)
                {
                    shippingFee = 0;
                }
                orderPromotionId = orderPromo.Id;
            }

            ApplyChange(new ShoppingCartPromotionCalulatedForOrderShipping(id, orderPromotionId));

            ApplyChange(new ShoppingCartCalculatedShipping(id, shippingMethodId, shippingFee));
        }
コード例 #2
0
        public void PreCalculate()
        {
            if (_items == null || _items.Count == 0)
            {
                throw new Exception("No item to pre-calculate");
            }
            var id = Guid.Parse(Id);

            long cartTax      = 0;
            long cartSubTotal = 0;

            if (_items != null && _items.Count >= 0)
            {
                cartSubTotal = _items.Sum(i => i.TotalPrice);
            }
            long cartDiscount     = 0;
            var  orderPromo       = OrderPromotionServices.CalculateForDiscount(id);
            var  orderPromotionId = Guid.Empty;

            if (orderPromo != null)
            {
                cartDiscount     = orderPromo.DiscountAmount;
                orderPromotionId = orderPromo.Id;
            }

            ApplyChange(new ShoppingCartPromotionCalulatedForOrderDiscount(id, orderPromotionId));

            var cartTotal = cartSubTotal - cartTax - cartDiscount - _voucherValue - _shippingFee;

            ApplyChange(new ShoppingCartPreCalculated(id, DateTime.Now, cartTax, cartDiscount, cartSubTotal, cartTotal));
        }
コード例 #3
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
                })
            });
        }