How to recognize removed item from order in webhook payload

How to recognize removed item from order in webhook payload

svukk
Shopify Partner
3 0 2

How to recognize removed item from order in webhook payload?

 

When you remove an item from order and then trigger webhook how to recognize removed item and ignore it?

 

Do you have any sugestion?

Replies 7 (7)

soenmez
Shopify Partner
50 6 17

Yes, the webhook payload for the order update event includes an array of line items, which represent the products or items in the order. When an item is removed from the order, it will no longer be included in this array.

 

To recognize the removed item, you can compare the line items in the webhook payload with the line items in the previous version of the order (which you can retrieve using the Orders Webhook API).

If a line item is present in the previous version of the order but not in the webhook payload, it has been removed.

 

Once you have identified the removed item, you can ignore it in your webhook handler code. Alternatively, you can take some action based on the removal of the item, such as updating inventory levels or sending a notification to the customer.

 

It's worth noting that if an item is removed from an order that has already been fulfilled, you may need to take additional steps to ensure that the fulfillment is properly updated or canceled.

Kenan Sönmez | CTO @ ECOM-SCHMIEDE
- War meine Antwort hilfreich? Klicke Like um es mich wissen zu lassen!
- Wurde deine Frage beantwortet? Markiere es als Akzeptierte Lösung
- https://www.ecom-schmiede.de/

svukk
Shopify Partner
3 0 2

Thank you for your quick response but for me this is not working.

 

I have order fulfillment webhook.

 

I created an test order with two items (Test and Test2). Before item fulfillment I click on "Edit order" and "Remove item" on item Test.

Now I clicked on "Fulfill item" on Test2 and in webhook payload I got line_items array with both items Test and Test2.

 

"line_items":[{"title":"Test","quantity":"1","price":"50.00","sku":"sku1","discount_allocations":[]},{"title":"Test 2","quantity":"1","price":"20.00","sku":"","discount_allocations":[],"fulfillment_status":"fulfilled"}]

 

No matter how I remove item, what is the status of order and so on always getting two items in list.


I don't know if I'm doing something wrong

 

soenmez
Shopify Partner
50 6 17

Can you please explain what exactly you want to achieve and maybe send through the exact webhooks (including version)?

Kenan Sönmez | CTO @ ECOM-SCHMIEDE
- War meine Antwort hilfreich? Klicke Like um es mich wissen zu lassen!
- Wurde deine Frage beantwortet? Markiere es als Akzeptierte Lösung
- https://www.ecom-schmiede.de/

svukk
Shopify Partner
3 0 2

I am making invoice for an order. I want make invoice with final state of order.

Invoice making action is connected to Order fulfillment webhook (2023-04 (Latest)), format JSON.

 

My customer sometimes make changes on order before fulfillment. For example, they remove an item from order and this item I do not wont on my invoice.

 

I am getting items for my invoice from "line_items" array. Problems occurs when an item is deleted from order it despite that that item is in "line_items". Is there any property that mark removed item as removed?

Lvn
Shopify Partner
1 0 0

Hi,
For Rest API, you need to check the Refunds in the order/updated webhook. A code like the one below can work by changing it according to the syntax of the programming language you use. newInventoryQuantityForItem returns the current count of the corresponding LineItem in the order:

var refundLineItemQuantity = 0;
var refundLineItemQuantityForInventory = 0;

foreach (var refund in orderShopify.Refunds)
{
foreach (var refundLineItem in refund.RefundLineItems)
{
if (refundLineItem.LineItemId.GetValueOrDefault() == lineItem.Id)
{
refundLineItemQuantity += refundLineItem.Quantity.GetValueOrDefault();

if (refundLineItem.RestockType == "cancel" || refundLineItem.RestockType == "return")
{
refundLineItemQuantityForInventory += refundLineItem.Quantity.GetValueOrDefault();
}
}
}
}

var newInventoryQuantityForItem = lineItem.Quantity - refundLineItemQuantityForInventory;



eba-tech
Shopify Partner
17 0 10

Hi @soenmez ,

I can concur with @svukk , you don't even need to look at a webhook, if you simply just go to the order .json, you still have the removed items in the line_items array.

To clarify further, on the Shopify UI the removed items appear grouped together (almost like another fulfillment) with the header 'Removed' where 'Fulfilled' normally is.

So the question still stands... how do we identify which ones are removed in this line_items array? There doesn't seem to be a clear marker.

eba-tech
Shopify Partner
17 0 10

Hi @svukk and @soenmez , you probably have figured it out by now, but I leave this here for other visitors.

 

As per my previous message, removed items do indeed still appear in the 'line_items' array with no obvious marker that they have been removed.

On further investigation, I think the solution lies in the 'refunds' array. If you cycle through the refunds.refund_line_items, you can match up the line_item_id and the quantity with the line_items array and diff them.

@soenmez can you confirm that this is indeed the correct way to determine removed items? If so, can we put in a feature request to flag refunded quantity directly in the line item in the 'line_items' array, this amount of processing is cumbersome for something so simple.