Kooboo.Commerce.Carts.ShoppingCartService.MigrateCart C# (CSharp) Method

MigrateCart() public method

public MigrateCart ( ShoppingCart from, ShoppingCart to ) : void
from ShoppingCart
to ShoppingCart
return void
        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);
        }

Usage Example

        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);
        }
All Usage Examples Of Kooboo.Commerce.Carts.ShoppingCartService::MigrateCart