public bool ApplyCoupon(ShoppingCart cart, string coupon)
        {
            if (String.IsNullOrWhiteSpace(coupon))
            {
                return false;
            }

            var oldCoupon = cart.CouponCode;

            cart.CouponCode = coupon;

            var context = PriceCalculationContext.CreateFrom(cart);
            new PriceCalculator().Calculate(context);

            if (context.AppliedPromotions.Any(p => p.RequireCouponCode && p.CouponCode == coupon))
            {
                return true;
            }

            cart.CouponCode = oldCoupon;

            _repository.Database.SaveChanges();

            return false;
        }
Beispiel #2
0
 public CartItemAdded(ShoppingCart cart, ShoppingCartItem item)
 {
     CartId = cart.Id;
     ItemId = item.Id;
     ProductId = item.ProductVariant.ProductId;
     ProductPriceId = item.ProductVariant.Id;
     Quantity = item.Quantity;
 }
 public CartItemQuantityChanged(ShoppingCart cart, ShoppingCartItem item, int oldQuantity)
 {
     CartId = cart.Id;
     ItemId = item.Id;
     ProductId = item.ProductVariant.ProductId;
     ProductPriceId = item.ProductVariant.Id;
     OldQuantity = oldQuantity;
     NewQuantity = item.Quantity;
 }
        public void AddItem(ShoppingCart cart, ShoppingCartItem item)
        {
            Require.NotNull(cart, "cart");
            Require.NotNull(item, "item");

            cart.Items.Add(item);
            _repository.Database.SaveChanges();

            Event.Raise(new CartItemAdded(cart, item), _instance);
        }
Beispiel #5
0
        public Order CreateFromCart(ShoppingCart cart, ShoppingContext context)
        {
            Require.NotNull(cart, "cart");
            Require.NotNull(cart.Customer, "cart.Customer", "Cannot create order from guest cart. Login is required.");
            Require.NotNull(cart.ShippingAddress, "cart.ShippingAddress", "Shipping address is required.");
            Require.That(cart.Items.Count > 0, "cart.Items", "Cannot create order from an empty cart.");

            // Recalculate price
            var pricingContext = new ShoppingCartService(_instance).CalculatePrice(cart, context);

            var order = new Order();
            order.ShoppingCartId = cart.Id;
            order.CustomerId = cart.Customer.Id;
            order.Coupon = cart.CouponCode;

            foreach (var item in pricingContext.Items)
            {
                var cartItem = cart.Items.FirstOrDefault(i => i.Id == item.ItemId);

                var orderItem = OrderItem.CreateFrom(cartItem, item.UnitPrice);
                orderItem.UnitPrice = item.UnitPrice;
                orderItem.Discount = item.Discount;
                orderItem.Subtotal = item.Subtotal;
                orderItem.Total = item.Subtotal - item.Discount;

                order.OrderItems.Add(orderItem);
            }

            order.ShippingAddress = OrderAddress.CreateFrom(cart.ShippingAddress);

            if (cart.BillingAddress != null)
            {
                order.BillingAddress = OrderAddress.CreateFrom(cart.BillingAddress);
            }

            order.ShippingCost = pricingContext.ShippingCost;
            order.PaymentMethodCost = pricingContext.PaymentMethodCost;
            order.Tax = pricingContext.Tax;
            order.Discount = pricingContext.TotalDiscount;

            order.Subtotal = pricingContext.Subtotal;
            order.Total = pricingContext.Total;

            Create(order);

            return order;
        }
        public ShoppingCartItem AddItem(ShoppingCart cart, Product product, ProductVariant productPrice, int quantity)
        {
            Require.NotNull(cart, "cart");
            Require.NotNull(product, "product");
            Require.NotNull(productPrice, "productPrice");
            Require.That(quantity > 0, "quantity", "Quantity should be greater than zero.");

            var item = cart.Items.FirstOrDefault(i => i.ProductVariant.Id == productPrice.Id);
            if (item == null)
            {
                item = new ShoppingCartItem(productPrice, quantity, cart);
                AddItem(cart, item);
            }
            else
            {
                ChangeItemQuantity(cart, item, item.Quantity + quantity);
            }

            return item;
        }
        public bool RemoveItem(ShoppingCart cart, int itemId)
        {
            var item = cart.Items.FirstOrDefault(i => i.Id == itemId);
            if (item == null)
            {
                return false;
            }

            cart.Items.Remove(item);
            _repository.Database.SaveChanges();

            Event.Raise(new CartItemRemoved(cart, item), _instance);

            return true;
        }
 public CartPriceCalculated(ShoppingCart cart, PriceCalculationContext context)
 {
     CartId = cart.Id;
     Context = context;
 }
Beispiel #9
0
 public CartCreated(ShoppingCart cart)
 {
     CartId = cart.Id;
 }
Beispiel #10
0
        public PriceCalculationContext CalculatePrice(ShoppingCart cart, ShoppingContext shoppingContext)
        {
            Require.NotNull(cart, "cart");

            var context = PriceCalculationContext.CreateFrom(cart);
            if (shoppingContext != null)
            {
                context.Culture = shoppingContext.Culture;
            }

            new PriceCalculator().Calculate(context);

            Event.Raise(new CartPriceCalculated(cart, context), _instance);

            return context;
        }
Beispiel #11
0
        public void ChangeItemQuantity(ShoppingCart cart, ShoppingCartItem item, int newQuantity)
        {
            Require.NotNull(cart, "cart");
            Require.NotNull(item, "item");
            Require.That(newQuantity > 0, "newQuantity", "Quantity should be greater than zero.");

            if (item.Quantity != newQuantity)
            {
                var oldQuantity = item.Quantity;
                item.Quantity = newQuantity;

                _repository.Database.SaveChanges();

                Event.Raise(new CartItemQuantityChanged(cart, item, oldQuantity), _instance);
            }
        }
Beispiel #12
0
 public ShoppingCartItem(ProductVariant variant, int quantity, ShoppingCart cart)
 {
     ProductVariant = variant;
     Quantity = quantity;
     ShoppingCart = cart;
 }
Beispiel #13
0
        public void MigrateCart(ShoppingCart from, ShoppingCart to)
        {
            Require.NotNull(from, "from");
            Require.NotNull(to, "to");

            if (from.Id == to.Id)
            {
                return;
            }

            foreach (var item in from.Items)
            {
                AddItem(to, item.ProductVariant.Product, item.ProductVariant, item.Quantity);
            }

            from.Items.Clear();
            _repository.Delete(from);
        }
Beispiel #14
0
 public CartExpired(ShoppingCart cart)
 {
     CartId = cart.Id;
 }
Beispiel #15
0
        public void ExpireCart(ShoppingCart cart)
        {
            Require.NotNull(cart, "cart");

            cart.SessionId = string.Format("EXPIRED_{0}_{1}", cart.SessionId, DateTime.UtcNow.Ticks.ToString());
            _repository.Database.SaveChanges();

            Event.Raise(new CartExpired(cart), _instance);
        }
Beispiel #16
0
        public void Delete(ShoppingCart cart)
        {
            Require.NotNull(cart, "cart");

            _repository.Delete(cart);
        }
Beispiel #17
0
        public void Create(ShoppingCart cart)
        {
            Require.NotNull(cart, "cart");

            _repository.Insert(cart);
            Event.Raise(new CartCreated(cart), _instance);
        }
Beispiel #18
0
 public void ChangeShippingMethod(ShoppingCart cart, ShippingMethod shippingMethod)
 {
     if (cart.ShippingMethod == null || cart.ShippingMethod.Id != shippingMethod.Id)
     {
         cart.ShippingMethod = shippingMethod;
         _repository.Database.SaveChanges();
     }
 }
Beispiel #19
0
 public void ChangeShippingAddress(ShoppingCart cart, Address address)
 {
     if (cart.ShippingAddress == null || cart.ShippingAddress.Id != address.Id)
     {
         cart.ShippingAddress = address;
         _repository.Database.SaveChanges();
     }
 }
Beispiel #20
0
        public bool RemoveProduct(ShoppingCart cart, int productPriceId)
        {
            var item = cart.FindItemByProductPrice(productPriceId);
            if (item == null)
            {
                return false;
            }

            return RemoveItem(cart, item.Id);
        }
        public static PriceCalculationContext CreateFrom(ShoppingCart cart)
        {
            var context = new PriceCalculationContext
            {
                Customer = cart.Customer,
                CouponCode = cart.CouponCode,
                BillingAddress = cart.BillingAddress,
                ShippingAddress = cart.ShippingAddress,
                ShippingMethod = cart.ShippingMethod
            };

            foreach (var item in cart.Items)
            {
                context.AddItem(item.Id, item.ProductVariant, item.Quantity);
            }

            return context;
        }