Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hey All,
I use the orders/updated webhook to know when an order has been modified on a store. The data I'm seeing when line item quantities are edited doesn't seem consistent. Specifically, when the quantity is increased on an existing line item (from 1 to 2, for example), the updated Order object shows the correct value (2) for the line item quantity, and the order subtotal and totals are updated to reflect 2 items instead of one. However, when I edit the order and decrease the line item quantity from 2 to 1, the order totals all still reflect a quantity of 2 and the actual line item quantity is still 2. The only difference is that there's now a refund object on the order. The actual data received for the different states of the order is below.
Why aren't the order and line item updated correctly when the quantity is decremented? Do I have to parse refund items to calculate this on my own? Thanks in advance.
orders/created
Here's the initial state of the order, with one line item with a quantity of 1 (unrelated data removed, unique values modified)
{ "id": 12345, "closed_at": null, "created_at": "2020-05-08T16:47:26-04:00", "updated_at": "2020-05-08T16:47:27-04:00", "test": false, "total_price": "80.37", "subtotal_price": "75.00", "total_weight": 680, "total_tax": "5.37", "taxes_included": false, "currency": "USD", "financial_status": "paid", "confirmed": true, "total_discounts": "0.00", "total_line_items_price": "75.00", "cancelled_at": null, "processed_at": "2020-05-08T16:47:26-04:00", "processing_method": "manual", "fulfillment_status": null, "line_items": [ { "id": 4567, "variant_id": 11223344, "title": "Product A", "quantity": 1, "sku": "AS00234-1", "variant_title": "black", "fulfillment_service": "manual", "product_id": 5566778899, "requires_shipping": true, "taxable": true, "variant_inventory_management": "shopify", "product_exists": true, "fulfillable_quantity": 1, "price": "75.00", "total_discount": "0.00", "fulfillment_status": null, } ], "refunds": [], }
orders/updated
Data received after setting the single line item's quantity to 2 (unrelated data removed, unique values modified)
{ "id": 12345, "closed_at": null, "created_at": "2020-05-08T16:47:26-04:00", "updated_at": "2020-05-08T16:55:28-04:00", "test": false, "total_price": "160.74", "subtotal_price": "150.00", "total_weight": 1360, "total_tax": "10.74", "taxes_included": false, "currency": "USD", "financial_status": "partially_paid", "confirmed": true, "total_discounts": "0.00", "total_line_items_price": "150.00", "cancelled_at": null, "processed_at": "2020-05-08T16:47:26-04:00", "fulfillment_status": null, "line_items": [ { "id": 4567, "variant_id": 11223344, "title": "Product A", "quantity": 2, "sku": "AS00234-1", "variant_title": "black", "fulfillment_service": "manual", "product_id": 5566778899, "requires_shipping": true, "taxable": true, "variant_inventory_management": "shopify", "product_exists": true, "fulfillable_quantity": 2, "price": "75.00", "total_discount": "0.00", "fulfillment_status": null, } ], "refunds": [], }
orders/updated
Here's the received data after editing the order and changing the single line item's quantity from 2 to 1 (unrelated data removed, unique values modified)
{ "id": 2298778779693, "closed_at": null, "created_at": "2020-05-08T16:47:26-04:00", "updated_at": "2020-05-08T17:07:19-04:00", "test": false, "total_price": "160.74", "subtotal_price": "150.00", "total_weight": 1360, "total_tax": "10.74", "taxes_included": false, "currency": "USD", "financial_status": "paid", "confirmed": true, "total_discounts": "0.00", "total_line_items_price": "150.00", "cancelled_at": null, "processed_at": "2020-05-08T16:47:26-04:00", "fulfillment_status": null, "line_items": [ { "id": 4567, "variant_id": 11223344, "title": "Product A", "quantity": 2, "sku": "AS00234-1", "variant_title": "black", "fulfillment_service": "manual", "product_id": 5566778899, "requires_shipping": true, "taxable": true, "variant_inventory_management": "shopify", "product_exists": true, "fulfillable_quantity": 1, "price": "75.00", "total_discount": "0.00", "fulfillment_status": null, } ], "fulfillments": [], "refunds": [ { "id": 98749874, "order_id": 12345, "created_at": "2020-05-08T17:07:18-04:00", "note": null, "processed_at": "2020-05-08T17:07:18-04:00", "restock": true, "refund_line_items": [ { "id": 9988776655, "line_item_id": 4567, "quantity": 1, "restock_type": "cancel", "subtotal": "75.00", "total_tax": "5.37", "line_item": { "id": 4567, "variant_id": 11223344, "quantity": 2, "sku": "AS00234-1", "variant_title": "black", }, } ], "transactions": [], "order_adjustments": [] } ], }
FWIW, I "solved" the problem by looping through all refunds and refund line items in the payload to adjust the order totals and line item quantities myself, then I process the updated order info as normal. I hope there's not some scenario I'm unaware of that would lead to this being a bad idea. It would be great if Shopify was consistent on updating those values, or at least provided an explanation regarding why/when they do get updated or not.
@aveshopstech Could I ask if you did have any issues doing this? I have a similar issue for the order/cancelled webhook, the line items quantity reflect the original quantity purchased, not the updated quantity, and there are multiple refund lines with different quantities for each time the item quantity was updated, making it difficult to know what the latest item quantity was.
Hey @travis24 ,
I've been using this method to "fix" order data for almost a year now, and as far as I know its been working correctly. I guess Shopify wants us to apply the refund data manually to determine the true state of the order. Good luck!
Thanks! I found that just getting the most recent refund and using the quantity for each item suits my use case, so we'll see how it goes...