November Happy Hour will be moved to Thursday December 5th.

Cart - CreateOrderForm - wrong calculate OrderFormId

Found in

EPiServer.Commerce 13.24.0

Fixed in

EPiServer.Commerce 13.26.0

(Or a related package)

Created

Sep 24, 2020

Updated

Nov 25, 2020

State

Closed, Fixed and tested


Description

For the multiple order forms in cart as below code, the CreateOrderForm() are returning same OrderFormId (-2)

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderGroupFactory = ServiceLocator.Current.GetInstance<IOrderGroupFactory>();
            var currentMarket = ServiceLocator.Current.GetInstance<ICurrentMarket>();
            ICart cart = orderRepository.LoadOrCreateCart<ICart>(CustomerContext.Current.CurrentContactId, "Default", currentMarket);
            if (cart != null)
            {
                orderRepository.Delete(cart.OrderLink);
            }
            cart = orderRepository.LoadOrCreateCart<ICart>(CustomerContext.Current.CurrentContactId, "Default", currentMarket);
            cart.Forms.Remove(cart.GetFirstForm()); // We always remove the form provided by default form in a new cart as we need to populate all order forms with a bunch of custom property values later on.
            cart.Forms.Add(orderGroupFactory.CreateOrderForm(cart)); // Expected orderformid is -2, which it is
            orderRepository.Save(cart);
            cart = orderRepository.LoadOrCreateCart<ICart>(CustomerContext.Current.CurrentContactId, "Default", currentMarket);
            cart.Forms.Add(orderGroupFactory.CreateOrderForm(cart)); //Expected orderformid is -3 which it is in 13.18.2, but in 13.24.0 this is assigned -2 which leaves us with two order forms with the same id
            cart.Forms.Add(orderGroupFactory.CreateOrderForm(cart)); // 13.18.2 assigns -4 and 13.24.0 assigns -3, here the incrementation seems to be working again

source: https://pastebin.com/3e48Lihr