Hi Kristoffer,
I was confused by this previously aswell, as the naming is very similar and almost exactly the same.
UpdateInventoryOrRemoveLineItem is the one you want if you're looking at altering the quantities on LineItems based on inventory available.
If you decompile the Epi code you will see that UpdateInventoryOrRemoveLineItem eventually calls ValidateLineItemInventory, which then reduces LineItem quantities if inventory numbers aren't enough to allocate that quantity. So you should see either line items reducing in quantity or being removed completely based on your inventory.
AdjustInventoryOrRemoveLineItem is more about allocating inventory to particular orders when they're completed/cancelled etc - more here under "Adjust inventory" :
https://world.episerver.com/documentation/developer-guides/commerce/orders/order-processing/
For your particular scenario you want UpdateInventoryOrRemoveLineItem - not sure of your issue as to why the quantity isn't being reduced though - maybe another process adjusting the quantity after UpdateInventoryOrRemoveLineItem finishes...?
Thanks,
Paul
Thanks Paul for a great summary!
Update actually reduces the quantity but does not remove the item if is not in stock which is the main problem. I will digg a bit more next week, all suggestions are welcome!
/Kristoffer
Hi!
I can't get this to work with standard code.
Adjust - Removes product if 0 in stock but does not reduce quantity if I have 1 and try to order 2.
Update - Reduces quantity if I have 1 and tries to buy to but does not remove the item if none in stock.
It seems to be the first return staement is this part that keeps the item in my cart when using Update.
bool flag = stock != null && stock.TrackInventory;
...
if (entryContent == null || lineItem.IsInventoryAllocated || !flag)
{
if (!string.IsNullOrEmpty(shipment.WarehouseCode) || flag)
return;
IWarehouse fulfillmentWarehouse = this._fulfillmentWarehouseProcessor.GetFulfillmentWarehouse(shipment);
if (fulfillmentWarehouse == null)
return;
shipment.WarehouseCode = fulfillmentWarehouse.Code;
}
If I use both Update and Adjust it actually looks like "in stock" is getting updated? If I have 0 and validates the cart I have 1 after?
Now I'm even more confused!?
/Kristoffer
UPDATE
If I have 0 in stock and are using Update I can add it to my cart but If I then update quantity to 2, the items get removed from the cart?
Why is that?
/Kristoffer
I guess all experts are back from Miami now? :-)
Can anyone shine a light on me?
Thanks!
Hi Kristoffer,
Sorry for the lack of response, unfortunately it wasn't due to being in Miami :)
Could you confirm again what the exact results are from your tests?
Can you also confirm you are only using UpdateInventoryOrRemoveLineItem now?
The only thing I can really suggest is to make sure you're not implementing your own IInventoryProcessor (although this might be unlikely), as this is the service used underneath by the UpdateInventoryOrRemoveLineItem extension method.
If you decompile the default implementation of IInventoryProcessor (DefaultInventoryProcessor), you'll see a private method in there that is the crux of the logic behind the removal of line items or the changing of line items quantities
private void ValidateLineItemInventory(IShipment shipment, ILineItem lineItem, Action<ILineItem, ValidationIssue> onValidationError, ICollection<ILineItem> lineItemsToDelete)
It does a number of different things in there, some of which are getting the inventory record for the current line item, and testing the current quantity in stock with the quantity requested on the line, but it also checks the allowed quantity for the line item, based on the min and max quantity settings provided on the inventory tab of the entry you're attempting to purchase.
By the sounds of your tests this doesn't sound to be the problem, but it may be worth looking into and checking the values (if any) of those fields.
Hope it helps,
Paul
Hi!
Thanks, I just found the problem.
We have created our own CreateLineItem since we have some special behavior to consider. In this function we had this line of code:
lineItem.IsInventoryAllocated = true;
Of course, this made the UpdateInventoryOrRemoveLineItem think that the line was ok. Before we never looked at the actual inventory, you could always add the item to the cart but we just changed this and when I removed the line above the item could not be added to cart.
When updating to 2 in the cart Epi standard behaviour was back and removed the item form the cart since there was non available.
I should have thought about this a long time ago...
Thanks!
Hi!
I have a strange, or maybe not, behaviour in the OrderValidationService.
For a variant I set in stock to 1, tracked inventory to true, backorder to 0, preorder to 0.
I then try to add 2 in my cart. Validation kicks in and lowers my quantity to 1. Perfect!
I then update then inventory item in stock to 0. Meaning there are no items available and it cannot be backordered or preordered.
I then add 1 to the cart and the validation does not remove it? Shouldn't the item be removed?
By the way, what is the differce between UpdateInventoryOrRemoveLineItem and AdjustInventoryOrRemoveLineItems?
Thanks!
/Kristoffer
UPDATE
AdjustInventoryOrRemoveLineItems removes the item from the cart but I doesn't reduce to 1 if I have 1 in stock and adds 2. I get validation error like AdjustedQuantityByAvailableQuantity, but quantity is still 2 in the cart. I'm getting confused. Should you run both?