如何将 shopify 订单设置为已履行?

How to set shopify order to fulfilled?

设置

我有一个 Shopify 商店有一个未完成的订单,并且可以访问商店的 REST API。

订单已发货,我有 tracking_numbertracking_urltransport_company

我想使用 REST API 将订单设置为已履行并将 tracking_numbertracking_urltransport_company 信息发送给客户。


代码

我在 Shopify 中有订单的 ID,order_id,这样我就可以得到订单的 fulfillment_orders 商品,然后从那里得到 fulfillment_idlocation_id,就像这样,

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']   

其中 shop_url 是连接到商店所需的 url。

到目前为止代码有效。

然后,我设置了payload,

payload = {
    "fulfillment": 
        {            
        "notify_customer": 'false',
        "location_id": location_id,        
        "tracking_info":{                
            "tracking_url": tracking_url,
            "tracking_company": transport_company,
            "tracking_number": tracking_number,            
            }
        }
    }

其中 location_id 是整数,其他变量是字符串。

当我随后运行以下请求将信息插入订单时,

r = requests.post(shop_url + '/fulfillments/' + fulfillment_id + '/update_tracking.json',
                 json=payload,headers=headers)

我得到一个 <Response [400]>.


问题

我做错了什么?

正确的做法是discussed here

假设您有 shopify 订单 ID order_idshop_url,以下代码会将订单设置为已完成,

# 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)