예제 #1
0
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as ShoppingCart;
            var cart = source as Core.ShoppingCart;

            // Update cart prices
            var priceContext = new Core.ShoppingCartService(context.ApiContext.Instance).CalculatePrice(cart, null);
            if (model.Items != null && model.Items.Count > 0)
            {
                foreach (var priceItem in priceContext.Items)
                {
                    var cartItem = model.Items.FirstOrDefault(it => it.Id == priceItem.ItemId);
                    cartItem.Subtotal = priceItem.Subtotal;
                    cartItem.Discount = priceItem.Discount;
                    cartItem.Total = priceItem.Subtotal - priceItem.Discount;
                }
            }

            model.ShippingCost = priceContext.ShippingCost;
            model.PaymentMethodCost = priceContext.PaymentMethodCost;
            model.Tax = priceContext.Tax;

            model.Subtotal = priceContext.Subtotal;
            model.TotalDiscount = priceContext.TotalDiscount;
            model.Total = priceContext.Total;

            return model;
        }
예제 #2
0
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as ShoppingCart;
            var cart  = source as Core.ShoppingCart;

            // Update cart prices
            var priceContext = new Core.ShoppingCartService(context.ApiContext.Instance).CalculatePrice(cart, null);

            if (model.Items != null && model.Items.Count > 0)
            {
                foreach (var priceItem in priceContext.Items)
                {
                    var cartItem = model.Items.FirstOrDefault(it => it.Id == priceItem.ItemId);
                    cartItem.Subtotal = priceItem.Subtotal;
                    cartItem.Discount = priceItem.Discount;
                    cartItem.Total    = priceItem.Subtotal - priceItem.Discount;
                }
            }

            model.ShippingCost      = priceContext.ShippingCost;
            model.PaymentMethodCost = priceContext.PaymentMethodCost;
            model.Tax = priceContext.Tax;

            model.Subtotal      = priceContext.Subtotal;
            model.TotalDiscount = priceContext.TotalDiscount;
            model.Total         = priceContext.Total;

            return(model);
        }
예제 #3
0
 public bool ApplyCoupon(int cartId, string coupon)
 {
     return(_context.Database.Transactional(() =>
     {
         var service = new Core.ShoppingCartService(_context.Instance);
         var cart = service.Find(cartId);
         return service.ApplyCoupon(cart, coupon);
     }));
 }
예제 #4
0
 public bool ApplyCoupon(int cartId, string coupon)
 {
     return _context.Database.Transactional(() =>
     {
         var service = new Core.ShoppingCartService(_context.Instance);
         var cart = service.Find(cartId);
         return service.ApplyCoupon(cart, coupon);
     });
 }
예제 #5
0
        public bool RemoveItem(int cartId, int itemId)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.Find(cartId);

            return(_context.Database.Transactional(() =>
            {
                return service.RemoveItem(cart, itemId);
            }));
        }
예제 #6
0
        public void ExpireCart(int cartId)
        {
            var service      = new Core.ShoppingCartService(_context.Instance);
            var shoppingCart = service.Query().Where(o => o.Id == cartId).FirstOrDefault();

            if (shoppingCart != null)
            {
                service.ExpireCart(shoppingCart);
            }
        }
예제 #7
0
        public int AddItem(int cartId, int productVariantId, int quantity)
        {
            var cartService = new Core.ShoppingCartService(_context.Instance);
            var cart        = cartService.Find(cartId);
            var variant     = new Kooboo.Commerce.Products.ProductService(_context.Instance).FindVariant(productVariantId);

            return(_context.Database.Transactional(() =>
            {
                return cartService.AddItem(cart, variant.Product, variant, quantity).Id;
            }));
        }
예제 #8
0
        public void ChangeShippingMethod(int cartId, int shippingMethodId)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.Find(cartId);
            var method  = _context.Database.Repository <Kooboo.Commerce.Shipping.ShippingMethod>().Find(shippingMethodId);

            _context.Database.Transactional(() =>
            {
                service.ChangeShippingMethod(cart, method);
            });
        }
예제 #9
0
        public int AddItem(int cartId, int productVariantId, int quantity)
        {
            var cartService = new Core.ShoppingCartService(_context.Instance);
            var cart = cartService.Find(cartId);
            var variant = new Kooboo.Commerce.Products.ProductService(_context.Instance).FindVariant(productVariantId);

            return _context.Database.Transactional(() =>
            {
                return cartService.AddItem(cart, variant.Product, variant, quantity).Id;
            });
        }
예제 #10
0
 public void ChangeItemQuantity(int cartId, int itemId, int newQuantity)
 {
     var service = new Core.ShoppingCartService(_context.Instance);
     var cart = service.Find(cartId);
     var item = cart.Items.FirstOrDefault(i => i.Id == itemId);
     if (item != null)
     {
         _context.Database.Transactional(() =>
         {
             service.ChangeItemQuantity(cart, item, newQuantity);
         });
     }
 }
예제 #11
0
        public int GetCartIdBySessionId(string sessionId)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.FindBySessionId(sessionId);

            if (cart == null)
            {
                cart = Kooboo.Commerce.Carts.ShoppingCart.Create(sessionId);
                service.Create(cart);
            }

            return(cart.Id);
        }
예제 #12
0
        public void ChangeItemQuantity(int cartId, int itemId, int newQuantity)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.Find(cartId);
            var item    = cart.Items.FirstOrDefault(i => i.Id == itemId);

            if (item != null)
            {
                _context.Database.Transactional(() =>
                {
                    service.ChangeItemQuantity(cart, item, newQuantity);
                });
            }
        }
예제 #13
0
        public int GetCartIdByCustomer(string email)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.FindByCustomerEmail(email);

            if (cart == null)
            {
                var customer = new Kooboo.Commerce.Customers.CustomerService(_context.Instance).FindByEmail(email);
                cart = Kooboo.Commerce.Carts.ShoppingCart.Create(customer);
                service.Create(cart);
            }

            return(cart.Id);
        }
예제 #14
0
        public void ChangeShippingAddress(int cartId, Address address)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart = service.Find(cartId);

            _context.Database.Transactional(() =>
            {
                var addr = GetOrCreateAddress(cart.Customer.Id, address);
                if (address.Id == 0)
                {
                    address.Id = addr.Id;
                }

                service.ChangeShippingAddress(cart, addr);
            });
        }
예제 #15
0
        public void ChangeShippingAddress(int cartId, Address address)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart    = service.Find(cartId);

            _context.Database.Transactional(() =>
            {
                var addr = GetOrCreateAddress(cart.Customer.Id, address);
                if (address.Id == 0)
                {
                    address.Id = addr.Id;
                }

                service.ChangeShippingAddress(cart, addr);
            });
        }
예제 #16
0
        public void MigrateCart(int customerId, string session)
        {
            var service     = new Core.ShoppingCartService(_context.Instance);
            var sessionCart = service.FindBySessionId(session);

            if (sessionCart == null)
            {
                return;
            }

            var customerCart = service.FindByCustomerId(customerId);

            if (customerCart == null)
            {
                var customer = _context.Database.Repository <Kooboo.Commerce.Customers.Customer>().Find(customerId);
                customerCart = Kooboo.Commerce.Carts.ShoppingCart.Create(customer, session);
                service.Create(customerCart);
            }

            service.MigrateCart(sessionCart, customerCart);
        }
예제 #17
0
 public void ExpireCart(int cartId)
 {
     var service = new Core.ShoppingCartService(_context.Instance);
     var shoppingCart = service.Query().Where(o => o.Id == cartId).FirstOrDefault();
     if (shoppingCart != null)
     {
         service.ExpireCart(shoppingCart);
     }
 }
예제 #18
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;
        }
예제 #19
0
 public bool RemoveItem(int cartId, int itemId)
 {
     var service = new Core.ShoppingCartService(_context.Instance);
     var cart = service.Find(cartId);
     return _context.Database.Transactional(() =>
     {
         return service.RemoveItem(cart, itemId);
     });
 }
예제 #20
0
        public void MigrateCart(int customerId, string session)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var sessionCart = service.FindBySessionId(session);
            if (sessionCart == null)
            {
                return;
            }

            var customerCart = service.FindByCustomerId(customerId);
            if (customerCart == null)
            {
                var customer = _context.Database.Repository<Kooboo.Commerce.Customers.Customer>().Find(customerId);
                customerCart = Kooboo.Commerce.Carts.ShoppingCart.Create(customer, session);
                service.Create(customerCart);
            }

            service.MigrateCart(sessionCart, customerCart);
        }
예제 #21
0
        public int GetCartIdBySessionId(string sessionId)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart = service.FindBySessionId(sessionId);
            if (cart == null)
            {
                cart = Kooboo.Commerce.Carts.ShoppingCart.Create(sessionId);
                service.Create(cart);
            }

            return cart.Id;
        }
예제 #22
0
        public int GetCartIdByCustomer(string email)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart = service.FindByCustomerEmail(email);
            if (cart == null)
            {
                var customer = new Kooboo.Commerce.Customers.CustomerService(_context.Instance).FindByEmail(email);
                cart = Kooboo.Commerce.Carts.ShoppingCart.Create(customer);
                service.Create(cart);
            }

            return cart.Id;
        }
예제 #23
0
        public void ChangeShippingMethod(int cartId, int shippingMethodId)
        {
            var service = new Core.ShoppingCartService(_context.Instance);
            var cart = service.Find(cartId);
            var method = _context.Database.Repository<Kooboo.Commerce.Shipping.ShippingMethod>().Find(shippingMethodId);

            _context.Database.Transactional(() =>
            {
                service.ChangeShippingMethod(cart, method);
            });
        }