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!

LineItem removed from PurchaseOrder after orderRepository.Save



I'm using Commercer 12.8.1

I have also tried to update to Commerce 12.17.2, but the issue is still the same with newer version.

The goal: Change products on a created Purchaseorder (Add / Remove / Update products in a placed order)

I'm doing this in steps.

  1. Getting a diff of changes
  2. Removing all old LineItems
  3. Adding the new set of items
  4. Adjusting any payments

I can see from the debugger and the Database the all new LineItems are saved to the DB after step 3. (If I return the function here all look as expected, except of course that payment totals are wrong.)

I then start with the process of getting new totals. 

When I here run 


some, but not all LineItems are removed from the order. This happens before I run ProcessPayments, but after that I have new totals calcualted.

We have a filter to exclude some items for discount calculations, and the items that are removed from the order are part of this filter. If I comment out the specific product types in this filter, the code works as intended and payments are handled correctly as well

However, this is a filter that we trigger only for discount calculation, and no breakpoints I set here are triggered when I step over the save frunction from OrderRepository.

I have also checked in the events triggered for PurchaseOrder.

In OnUpdating all LineItems are present, but in OnUpdated some are removed. 

This only seem to happen for products that are part of our custom filter, but I can't understand why this should be triggerd in Save at a specific time, sine it does not always do so. And IF it is this filter that is getting triggered from the Save method, why I cant see this from breakpoints or logging when I run the application?

In what cases are LineItems removed from an order when _orderRepository.Service.Save(order);  is called?

I have also setup a custom ILineItemValidator that simply adds logging if a LineItem is removed due to being invalid in some way, but this is not triggered before the removal of the LineItem either.

Is there any other form of LineItemValidation that is done in the saving of PurchaseOrder that might affect my order that I can look at for more info? 

We do not use Inventory tracking for any productsin the catalog.

Mar 18, 2019 17:48

Hello Quan!

I looked at this thread as well, but I'm not sure this is the same issue.

I'm running Commerce in a verion that is higher then when the fix mentioned was implemented.

Also, I get a consistent behaviour for products that are part of our filter.

I think the issume might be in some workflow related to missmatch in OrderTotal and the sum of the LineItems, but I'm not sure on how to cntinue to pinpoint the problem.

This is currently my LineItemValidator that try to catch when it item is removed, but it doesn't trigger. Is there another validator I should try and setup logging for to spot the problem? 

 [ServiceConfiguration(ServiceType = typeof(ILineItemValidator), Lifecycle = ServiceInstanceScope.Singleton)]
    public class CustomLineItemValidator : ILineItemValidator
        protected readonly ILogger _log = IfLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private readonly ILineItemValidator lineItemValidator;

        public CustomLineItemValidator(IContentLoader contentLoader, ReferenceConverter referenceConverter, ICatalogSystem catalogSystem, IPublishedStateAssessor publishedStateAssessor)
            this.lineItemValidator = new DefaultLineItemValidator(contentLoader, referenceConverter, catalogSystem, publishedStateAssessor);

        public bool Validate(ILineItem lineItem, IMarket market, Action<ILineItem, ValidationIssue> onValidationError)
            return Validate(lineItem, market.MarketId, onValidationError);

        public bool Validate(ILineItem lineItem, MarketId marketId, Action<ILineItem, ValidationIssue> onValidationError)
            return lineItemValidator.Validate(lineItem, marketId, (line, validationIssue) => {
                onValidationError(line, validationIssue);
                LogValidationError(line, validationIssue);

        private void LogValidationError(ILineItem lineItem, ValidationIssue validationIssue)
            _log.Error($"LineItem {lineItem.Code} is not valid: {validationIssue.ToString()}");

You mention OrderStatusManager.CompleteOrderShipment is the trigger. How can I setup a check at this event as well? 

Mar 18, 2019 18:36

Hello again

I have found a workaround for our issue, but I'm still unclear to why this works.

I split up the function to 3 seprate parts, and now make seprate API-calls for each.

This sloves this issue, and I can have the same product filter as before. 

However, I don't see why this should solve the issue, since it is the exact same code that is executed, only with a little more time between calls. 

This gives the impression that some actions in the order managment doesn't have time to complete before the next call is made? 

It is thill the same user who makes the new API-calls with the same access rights. 

Is it possible that workflows that are triggered from order operations will not have fully completed before next line of code is evalutaed? 

Is there a way to fully wait for workflows to complete?

Mar 19, 2019 11:50

What I was trying to say is IOrderRepository.Save can't and won't remove items from order, the code that remove them is something else. You should scope it down to find out which code does that, and why. Most common reason is out of stock, but there might be other reasons.

Mar 19, 2019 11:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.