設定
我有一個未完成訂單的 Shopify 商店,并且可以訪問商店的 REST API。
訂單已發貨,我有它的tracking_number,tracking_url和transport_company.
我想使用 REST API 來設定要履行的訂單并將tracking_number,tracking_url和transport_company資訊發送給客戶。
代碼
我在 Shopify 中有訂單的 id order_id,這樣我就可以得到訂單的fulfillment_orders商品,然后從那里得到fulfillment_id,location_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 = {
"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]>.
問題
我究竟做錯了什么?
uj5u.com熱心網友回復:
正確的方法在這里討論。
假設您有 shopify 訂單 IDorder_id和shop_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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/407763.html
標籤:
