Issue with IShipment.LineItems.Remove method

Vote:
 

Hi,

According to the documentation you can remove line items from a shipment uisng the Remove method:

shipment.LineItems.Remove(ILineItem)


In my case that is not working. 

Let's say I have an order that consists of 4 items of product Y, and I want to remove 2 of them. 

What happens is that only 1 gets removed. 

Note: In our site we have our line items in a flat structure, this means that for the example order we will have 4 line items for same sku with qantity 1.

I would consider this a bug, do you agree?

What I would like to see in the api is a method like this:

shipment.LineItems.Remove(LineItemId)



#172321
Nov 30, 2016 9:11
Vote:
 

Hi,

That's pretty strange. Can you provide a test case to reproduce the bug? We will work on it ASAP.

Regards,

/Q

#172324
Nov 30, 2016 9:24
Vote:
 
  • Create a cart with 4 line items of same sku (same code) with quantity 1
  • Save the cart as a purchase order
  • In the complete shipment event (setting order to Complete), remove 2 of the line items
  • Go to commerce manager and see that only 1 is removed

The code for adding items to the cart is using the old api:

orderForm.LineItems.Add(lineItem1,false);

Note: the flag 'LineItemRollup" is set to false.

#172325
Edited, Nov 30, 2016 9:32
Vote:
 
<p>I am also able to reproduce this in&nbsp;QuickSilver.</p>
#172333
Nov 30, 2016 12:41
Vote:
 

Wait ... what... You added the items using old APIs and removed them by the new APIs? No candies for you!

I will try to verify this issue and file a bug when I have time.

Regards,

/Q

#172359
Dec 01, 2016 9:43
Vote:
 

Haha, we're slowly moving our code over to the new api, but that is a process that takes time ;) Thanks!

#172360
Dec 01, 2016 9:45
Vote:
 

Btw: Adding and removing items in QuickSilver using new api result in the same error ;)

#172361
Dec 01, 2016 9:50
Vote:
 

Hi Mari,

You're using old api to add lineitem to OrderForm, so could you try using old api to remove lineitem

OrderForm.RemoveLineItemFromShipments(lineItem1)

Or you could use new api to add lineitem to shipment or remove lineitem from shipment as document you mentioned

//adding
var lineItem = orderFactory.CreateLineItem("code");
var shipment = GetFirstShipment();
cart.AddLineItem(shipment, lineItem); 

//removing
cart.GetFirstShipment().Remove(lineItem);
#172362
Edited, Dec 01, 2016 9:52
Vote:
 
<p>Son Do: the bug exists in new api as well.</p>
#172363
Dec 01, 2016 9:53
Vote:
 

Could you show your code? Or step to reproduce on Quicksilver, we will verify easier.

#172364
Dec 01, 2016 9:58
Vote:
 

Sample code to reproduce in QuickSilver (using an existing order). First, add 4 line items of same sku:

var orderRepo = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderFactory = ServiceLocator.Current.GetInstance<IOrderFactory>();
            var orderId = 9;

            var order = orderRepo.Load<IPurchaseOrder>(orderId);
            var form = order.GetFirstForm();
            var shipment = form.Shipments.First();

            var lineItemOne = orderFactory.CreateLineItem("SKU-21320033");
            lineItemOne.Quantity = 1;
            var lineItemTwo = orderFactory.CreateLineItem("SKU-21320033");
            lineItemTwo.Quantity = 1;
            var lineItemThree = orderFactory.CreateLineItem("SKU-21320033");
            lineItemThree.Quantity = 1;
            var lineItemFour = orderFactory.CreateLineItem("SKU-21320033");
            lineItemFour.Quantity = 1;

            shipment.LineItems.Add(lineItemOne);
            shipment.LineItems.Add(lineItemTwo);
            shipment.LineItems.Add(lineItemThree);
            shipment.LineItems.Add(lineItemFour);

            orderRepo.Save(order);

Next, we try to remove 2 of them:

 var orderRepo = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderId = 9;

            var order = orderRepo.Load<IPurchaseOrder>(orderId);
            var form = order.GetFirstForm();
            var shipment = form.Shipments.First();

            var lineItemsToRemove = shipment.LineItems.Where(l => l.Code == "SKU-21320033").Take(2).ToList();

            foreach (var lineItem in lineItemsToRemove)
            {
                shipment.LineItems.Remove(lineItem);
            }

            orderRepo.Save(order);

Result: Instead of removing 2 items, only 1 is removed. If you try to remove 4, same result: only 1 is removed.

#172369
Dec 01, 2016 12:39
Vote:
 

I created a bug for it - will keep you posted when we investigate. Thanks for bringing this into our attention.

/Q

#172376
Dec 01, 2016 15:13
Vote:
 

Thanks, Quan!

#172411
Dec 02, 2016 9:10
Vote:
 

Hi there,

We got this issue before when try to remove line item from the shipment too. It looks similar issue you have.

You can change the code remove line item like that

foreach (var lineItem in giftItems)
{
   shipment.LineItems.Remove(shipment.LineItems.FirstOrDefault(l => l.LineItemId == lineItem.LineItemId));
}

Hope that fix your issue!

/Tuan

#172420
Dec 02, 2016 11:27
Vote:
 

Tuan, yes that will work as a temporary workaround until Episerver fixes the bug. Thanks!

#172421
Dec 02, 2016 11:34