Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Order cancel.json refund line items legacy_restock doesn't work

Solved

Order cancel.json refund line items legacy_restock doesn't work

ska-v
Shopify Partner
6 0 4

We are using multiple locations shop, with only one location assigned to online sales and one or more physical locations wich could fulfill, with customer availability to cancel order in an hour, to permit this, we implemted a custom app that sent via Rest API a cancel.json order and we need also to restock all products qty commited.

Shopfy doesn't allocate stock until order fulfill, in fact we found in product inventory history the right quantities on commited column and a decrease in availability column, but when make a call, we have a decrease on commited column but we didn't find any increment on availabilty column, the only way to re-increment the qty was set location id on the call and use "cancel" on restock type: but we didn't know the real location id where Shopify have decrease the availability.

How to invoke the right cancel with restock line item call?

Accepted Solution (1)

jon551
Shopify Staff
28 7 5

This is an accepted solution.

Hi Ska-V,

 

When cancelling orders via REST API the key thing to do is to use the Refund API's 'calculate' endpoint to generate the refund JSON before performing the cancel operation, and using the returned generated refund object inside the cancellation request to have the restocking and refund happen at the same time as the order cancellation.

 

Here's the documentation about the Refund API's 'calculate' endpoint: Calculate Refunds - Shopify Dev Docs

 

The Refund API's 'calculate' endpoint will automatically provide the location id for each line item within the reply so the location doesn't need to be specified. The main thing to remember is that when calling the 'calculate' endpoint each line item needs to be specified with the line_item_id, quantity, and restock_type.

 

So as an example, to cancel an order with id 22222 and restock all the items the first step is to POST to the calculate endpoint at /admin/api/2022-07/orders/22222/refunds/calculate.json with a payload including each of the line items that will be restocked and refunded like this (there's no quick 'restock all' option anymore'):

 

{
    "refund": {
        "refund_line_items": [
            {
                "line_item_id": 12345,
                "quantity": 1,
                "restock_type": "cancel"
            },
            {
                "line_item_id": 67890,
                "quantity": 1,
                "restock_type": "cancel"
            }
        ]
    }
}

 

That will return a big refund block of JSON with a refund object that includes many additional details.

 

The returned refund object can and should be used in its entirety as the 'refund' parameter when calling the /admin/api/2022-07/orders/22222/cancel.json endpoint and automatically includes the correct location_id's for each item. When that returned refund block is included in the cancellation request the restocking and refund will happen at the same time as the cancellation.

 

A good reference about the /cancel.json endpoint and using the refund block within it is here: [Cancel Orders with REST - Shopify Dev Docs]

 

One important thing to note is that the response from the /calculate.json endpoint includes a transactions object with "kind": "suggested_refund", which must to be changed to "kind" : "refund" or the entire request to /cancel.json will fail with a 422 Unprocessable Entity error.

 

Hope you have a great day,

Jon551 | API Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Reply 1 (1)

jon551
Shopify Staff
28 7 5

This is an accepted solution.

Hi Ska-V,

 

When cancelling orders via REST API the key thing to do is to use the Refund API's 'calculate' endpoint to generate the refund JSON before performing the cancel operation, and using the returned generated refund object inside the cancellation request to have the restocking and refund happen at the same time as the order cancellation.

 

Here's the documentation about the Refund API's 'calculate' endpoint: Calculate Refunds - Shopify Dev Docs

 

The Refund API's 'calculate' endpoint will automatically provide the location id for each line item within the reply so the location doesn't need to be specified. The main thing to remember is that when calling the 'calculate' endpoint each line item needs to be specified with the line_item_id, quantity, and restock_type.

 

So as an example, to cancel an order with id 22222 and restock all the items the first step is to POST to the calculate endpoint at /admin/api/2022-07/orders/22222/refunds/calculate.json with a payload including each of the line items that will be restocked and refunded like this (there's no quick 'restock all' option anymore'):

 

{
    "refund": {
        "refund_line_items": [
            {
                "line_item_id": 12345,
                "quantity": 1,
                "restock_type": "cancel"
            },
            {
                "line_item_id": 67890,
                "quantity": 1,
                "restock_type": "cancel"
            }
        ]
    }
}

 

That will return a big refund block of JSON with a refund object that includes many additional details.

 

The returned refund object can and should be used in its entirety as the 'refund' parameter when calling the /admin/api/2022-07/orders/22222/cancel.json endpoint and automatically includes the correct location_id's for each item. When that returned refund block is included in the cancellation request the restocking and refund will happen at the same time as the cancellation.

 

A good reference about the /cancel.json endpoint and using the refund block within it is here: [Cancel Orders with REST - Shopify Dev Docs]

 

One important thing to note is that the response from the /calculate.json endpoint includes a transactions object with "kind": "suggested_refund", which must to be changed to "kind" : "refund" or the entire request to /cancel.json will fail with a 422 Unprocessable Entity error.

 

Hope you have a great day,

Jon551 | API Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog