Set-up
I have a Shopify store with an unfulfilled order and have access to the store’s REST API.
The order has been shipped and I have its tracking_number
, tracking_url
and the transport_company
.
I want to use the REST API to set the order to fulfilled and send the tracking_number
, tracking_url
and transport_company
info to the customer.
Code
I have the order’s id in Shopify, the order_id
, such that I can get the order’s fulfillment_orders
item and from there the fulfillment_id
and location_id
like so,
fulfillment_orders = requests.get(shop_url + '/orders/'+ order_id +'/fulfillment_orders.json').json()
fulfillment_id = str(fulfillment_orders['fulfillment_orders'][0]['id'])
location_id = requests.get(shop_url + '/locations.json').json()['locations'][0]['id']
where shop_url
is the url needed to connect to the store.
So far the code works.
Then, I set up the payload,
payload = {
"fulfillment":
{
"notify_customer": 'false',
"location_id": location_id,
"tracking_info":{
"tracking_url": tracking_url,
"tracking_company": transport_company,
"tracking_number": tracking_number,
}
}
}
where location_id
is an integer and the other variables are strings.
When I subsequently run the following request to insert the information into the order,
r = requests.post(shop_url + '/fulfillments/' + fulfillment_id + '/update_tracking.json',
json=payload,headers=headers)
I get a <Response [400]>
.
Question
What am I doing wrong?
2
Answers
The correct way to do it is discussed here.
Assuming you have the shopify order id
order_id
and theshop_url
, the following code will set an order as fulfilled,For me it was a mix of solutions the accepted answer sadly didnt work straight away for me.
First of all you need the fulfillments of a given order – this is the endpoint:
Be aware there can be more than one fulfillment per order. I recommend to use the status=open flag. Otherwise you also get previously closed fulfillments like e.g. if you fulfilled it earlier and set it back to unfulfilled. Since they are closed I wouldn’t request them. Funfact you can’t open this endpoint via Firefox use chrome or a real api platform instead.
You get the Location ID from this endpoint.
Here comes the next pitfal. For me it was not enough to simply call
api/orders/fulfillments.json
. I had to provide theORDER-ID
again like in the first call.This is the payload: