Home > database >  How to set shopify order to fulfilled?
How to set shopify order to fulfilled?

Time:01-10

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?

CodePudding user response:

The correct way to do it is discussed here.

Assuming you have the shopify order id order_id and the shop_url, the following code will set an order as fulfilled,

# get order fulfillment id 
fulfillment_orders = requests.get(shop_url   '/orders/'  order_id  '/fulfillment_orders.json').json()     
fulfillment_id = fulfillment_orders['fulfillment_orders'][0]['id']
location_id = requests.get(shop_url   '/locations.json').json()['locations'][0]['id']   

# get orders fulfillment line items 

# note: if you want to fulfill only certain products in the order with the tracking code,
# here's where you select these products. Right now, the code isn't doing this 

fulfillment_order_line_items = []

for item in fulfillment_orders['fulfillment_orders'][0]['line_items']:
    
    fulfillment_order_line_items.append(
            {
            'id': item['id'],
            'quantity': item['quantity'],
            }
        )         

# set up payload
payload = {
    "fulfillment": 
        {            
            "notify_customer": 'false',
            "location_id": location_id,        
            "tracking_info":
                {                
                "url": tracking_url,
                "company": transport_company,
                "number": tracking_number,            
                },
            "line_items_by_fulfillment_order": [
                {
                    "fulfillment_order_id": fulfillment_id,
                    "fulfillment_order_line_items": fulfillment_order_line_items
                }
            ]            
        }
    }
    
# parse tracking info
r = requests.post(shop_url   '/fulfillments.json',
                 json=payload,headers=headers)
  •  Tags:  
  • Related