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; }
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); }
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); })); }
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); }); }
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); })); }
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); } }
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; })); }
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); }); }
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; }); }
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); }); } }
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); }
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); }
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); }); }
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); }
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 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); }); }
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); }
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; }
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; }
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); }); }