Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

cart.SaveAsPurchaseOrder throw new InvalidOperationException("The cart is empty")



We are using Episerver Commerce version 10.2.3.

Often we can create our orders without problems, but for some orders we get the exception, InvalidOperationException("The cart is empty"), when calling the SaveAsPurchaseOrder method on the cart in the DIBSPayment.aspx.cs class from the EPiServer.Business.Commerce.Payment.DIBS provider.

We have added some logging of the cart id and the number of line items returned by cart.GetAllLineItems() right before calling the SaveAsPurchaseOrder. The number of line items from the logging is not 0 and also if we look up the cart id in the Commer Manager we can also see that the cart contains line items.

Right now we have no clue why this is happening, so hope someone can help.

What we have seen so far, it is only happening for authenticated users, but not 100% sure this has something to do with it.



An example from the log:

cart.GetAllLineItems().Count() // returns the value of 8

cart.OrderForms.Count // returns the value of 1

cart.OrderForms.Sum(x => x.LineItems.Count) // return the value 0

cart.OrderForms.Any(order => order.LineItems.Any()) // return false

Edited, Jun 01, 2017 22:12

This is probably caused as you have your lineitems in your orderform, but you yet to add them to a shipment. LineItems should belong to shipments, otherwise it can cause the problem as you saw. 

Jun 02, 2017 10:07

Thank you Quan.

Yes I was also looking at something about the shipment, but strange that we only get the error sometimes and we only have one way of adding products to the cart.

I tried decompiling the GetAllLineItems method, which in our case returned the value of 8 and I can see the method is looking at the shipment.

But if I decompile the SaveAsPurchaseOrder method I can see it's calling a private method called IsEmpty and the implemetation of the method is:

private bool IsEmpty()
      return !this.OrderForms.Any<OrderForm>((Func<OrderForm, bool>) (order => order.LineItems.Any<LineItem>()));

So as I can see it this method is not lokking at the shipments?

And still it's only happening sometimes.

Edited, Jun 02, 2017 10:17
<p>They way we add products to the cart&nbsp;like this:</p> <p></p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">cart.AddLineItem(lineItem, _orderGroupFactory);</pre> <p></p> <p>By looking at the decompiled version of AddLineItem it seems like this is adding the line items to the shipment:</p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">public static void AddLineItem(this IOrderGroup orderGroup, ILineItem lineItem, IOrderGroupFactory orderGroupFactory) { Validator.ThrowIfNull("lineItem", (object) lineItem); IOrderForm orGetFirstForm = IOrderGroupExtensions.CreateOrGetFirstForm(orderGroup, orderGroupFactory); IOrderGroupExtensions.CreateOrGetFirstShipment(orderGroup, orGetFirstForm, orderGroupFactory).LineItems.Add(lineItem); }</pre> <p></p>
Jun 02, 2017 11:38


Do you use the new cart system (serializable carts) or still the old one? (If the SaveAsPurchaseOrder method calls the IsEmpty() method then I guess you're using the old cart system).

About cart.GetAllLineItems(), yes it only gets line items in shipments in 10.2.3 version. It was updated in 10.3.0 version, so it gets line items of order forms if there's not any shipment in the cart.

Jun 07, 2017 6:28

Hi Bien,

Yes we are still using the old cart system and not the new serializable carts.

In our case the cart.GetAllLineItems() returns the correct number of items and we can also see from our log that the cart has a shipment with line items.

The IsEmpty() method on the other hand is returning true and from our log we can see that the cart has one order form but that order form does not hold any line items.

By looking at the decompiled IsEmpty() method from my post above I can see that this method is not looking for line items on the shipment but only on the order forms?

Jun 07, 2017 8:25

That's true, IsEmpty() is looking for line items on order forms only. In the old cart system, the cart.AddLineItem() method adds the line item to the first shipment and then it syncs back to the line item collection of the order form containing the shipment. So normally OrderForm.LineItems should contain the newly added line item as well. But it seems that does not happen in your case. Could you check the the parent of the shipment to see if it's null? Is there any other operation on the cart before adding the line item?

Jun 07, 2017 9:24

We got the support case reported by you - we will work on it as soon as we have someone available.

Jun 07, 2017 15:31


I was able to spend some time to look into this case, and this is my initial finding. Without any ability to reproduce/debug this issue, this is largely my suspection, so you'll probably need to confirm yourself.

To explain things, we need to know that there are two APIs for the catalog system: the concrete classes (CartHelper, Cart, OrderGroup,...), and the (new) abstraction APIs (IOrderForm, IShipment, IOrderGroupFactory, ...). We - as Episerver - worked hard to ensure that these two systems are in sync, i.e., if you create an order using the concrete classes, you should be able to load it again using the abstraction APIs. However, it's not recommended to mix both of them. In your case, it seems that you are using CartHelper to create a cart, but then use the abstraction APIs to add new items to them. This can be problematic.


In the concrete classes the LineItems belong to the OrderForm, and then distributed to the Shipments. This has been problematic, so in the new abstraction APIs, the LineItems belong to Shipment, which allows much easier and robust approach to the LineItem-Shipment relationship. However, because the underlying/default implementation, we need to do some "magic" to sync between the abstraction, and the implementation. It works just fine if it's new abstraction APIs all the way (since you load the cart, add items to it, and save it back). Or concrete classes all the way. Mixing them might break the synchronization (or "magic") between these two APIs. If your cart was properly "initialized" - aka it already contains an OrderForm and a Shipment, then things might work for you. However if it's empty, then AddLineItem will add "in-memory" instances of those, and that can be out-of-sync with underlying data, causing the problem.

So what is the fix? The best way is that choose one way and stick with it. You might use the CartHelper.AddEntry method to add the entry to cart. Or you move entirely to the new abstraction APIs (IOrderRepository all the way) - which is the preferred way. 

A quick fix should be this (again, untested, as I was unable to reproduce the problem)

if (cartHelper.Cart.OrderForms.Count == 0) 

// create a new one


orderForm = new OrderForm();     

orderForm.Name = this.Cart.Name;     



if (cartHelper.Cart.OrderForms[0].Shipments.Count == 0)


var shipment = new Shipment();     



Before calling the AddLineItem. However this is just a workaround - for long term solution I would suggest to move to abstraction APIs entirely.

Edited, Jun 07, 2017 17:50
<p>Okay. We have found one problem somehow relating to basket merge at login, but it might just be that we tried to merge with a null basket. That fixe a problem with shipment method id not being set, and that causing the missing lineitems after running the&nbsp;<span>CartCheckOutWorkflow.,</span><br /><br />But now we have seen some problems with the same result, that&nbsp;<br />OrderGroupWorkflowManager.RunWorkflow(cart, OrderGroupWorkflowManager.CartCheckOutWorkflowName, true, isIgnoreProcessPayment);<br />causes lineitems to disapear. It is properly relatede to some wrong data in the basket before that call, like the previous invalid shipment method id (even though that did not cause an exception in the flow!)<br />Now we see that CustomerName is empty, but we don't know what could cause that, because the basket was loaded correctly with the userid. Does this tell you anything?</p> <p>We are using the abstract api to all basket modifications, and we are only using the code you provided in&nbsp;DIBSPayment with CartHelper - isn't that what you recommend?</p>
Jun 08, 2017 8:15

If you are using abstraction APIs everywhere - then it's good (you probably want to upgrade and use the version of DIBS which uses abstraction APIs as well). I asked our developer support engineer to ask for your code and database to investigate further. 

Edited, Jun 08, 2017 10:58
<p>Where do I get a version of the DIBS with the abstraction api?</p>
Jun 08, 2017 15:00

You must be using 10.5.0 or later. 

Jun 08, 2017 15:05

Is there any way to disable auto basket merge after login?
We have so many problems that seems to be somehow related to that.

I have this thread but how do I add the event?
"Given you already register the event in Global.asax, you need to add a method like this:" ???

Jun 12, 2017 13:48

Here's something you can try: 

Jun 12, 2017 15:11
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.