Multiple forms cart is improperly merged

Found in

EPiServer.Commerce 10.4.3

Created

Apr 11 2017

Updated

Apr 14 2017

Fixed in

EPiServer.Commerce 10.5.0

Area

Core

State

Closed, Fixed and tested

Voting

Affected? You can raise the priority of a bug by voting for it. By doing so, you will be helping our developers to choose which bug to fix next.

Voting is disabled for closed bugs.

0

Description

When a cart has multiple forms, merging the cart (by logging in) can result in incorrect line item information.

Test to reproduce:

class When_merge_multiple_forms_cart : IntegrationCartTestBase
    {
        static VariationContent _entry1;
        static VariationContent _entry2;
        static VariationContent _entry3;
        static Guid _customerId;
        static Guid _anonymousId;
 
        protected static ICart CustomerCart;
 
        Establish context = () =>
        {
            _customerId = Guid.NewGuid();
            _anonymousId = Guid.NewGuid();
            _entry1 = CreateContent<VariationContent>(content => content.Code = "entry1");
            _entry2 = CreateContent<VariationContent>(content => content.Code = "entry2");
            _entry3 = CreateContent<VariationContent>(content => content.Code = "entry3");
 
            SetupCustomerCart();
            SetupAnonymousCart();
        };
 
        Because of = () => MergeCarts();
 
        protected static void SetupCustomerCart()
        {
            var customerCart = OrderRepository.LoadOrCreateCart<ICart>(_customerId, Cart.DefaultName);
            customerCart.Forms.Clear();
            
            AddLineItem(customerCart, _entry1.Code, "OrderForm1");
            AddLineItem(customerCart, _entry2.Code, "OrderForm2");
 
            OrderRepository.Save(customerCart);
        }
 
        protected static void SetupAnonymousCart()
        {
            var anonymousCart = OrderRepository.LoadOrCreateCart<ICart>(_anonymousId, Cart.DefaultName);
            anonymousCart.Forms.Clear();
 
            AddLineItem(anonymousCart, _entry1.Code, "OrderForm1");
            AddLineItem(anonymousCart, _entry3.Code, "OrderForm2");
 
            OrderRepository.Save(anonymousCart);
        }
 
        private static void AddLineItem(ICart cart, string code, string form)
        {
            var item = cart.CreateLineItem(code, OrderGroupFactory);
            item.Quantity = 1;
            var orderForm = cart.CreateOrderForm();
            orderForm.Name = form;
            var shipment = cart.CreateShipment();
            orderForm.Shipments.Add(shipment);
            shipment.LineItems.Add(item);
            cart.Forms.Add(orderForm);
        }
 
        protected static void MergeCarts()
        {
            var cartMigrator = new CartMigrator(OrderRepository);
            cartMigrator.MigrateCarts(_anonymousId, _customerId);
            CustomerCart = OrderRepository.Load<ICart>(_customerId, Cart.DefaultName).First();
        }
 
        private It should_merge_properly =
            () =>
                CustomerCart.Forms.First(f => f.Name == "OrderForm2")
                    .Shipments.First()
                    .LineItems.ShouldContain(l => l.Code == _entry3.Code);
    }

Comments