curl 如何将Shopify订单设置为已完成?

rta7y2nd  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(132)

设置

我有一个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 = {
    "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]>

问题

我做错了什么?

dzhpxtsq

dzhpxtsq1#

正确的方法是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)

字符串

332nm8kg

332nm8kg2#

对我来说,这是一个混合的解决方案,公认的答案可悲的是没有立即为我工作。
首先,你需要完成给定的订单-这是终点:

GET  https://<<YOUR-SHOP>>/admin/api/2022-04/orders/<<ORDER-ID>>/fulfillment_orders.json?status=open

字符串
请注意,每个订单可能有多个履行。我建议使用status=open标志。否则,您也会获得以前关闭的履行,例如,如果您早些时候履行了它,并将其设置为未履行。由于它们已关闭,我不会请求它们。Funfact,您无法通过Firefox打开此端点,请使用Chrome或真实的API平台。

GET  https://<<YOUR-SHOP>>/admin/api/2022-04/locations.json


您从此端点获取位置ID。
接下来是下一个pitfal。对我来说,简单地调用api/orders/fulfillments.json是不够的。我必须像第一次调用一样再次提供ORDER-ID

POST  https://<<YOUR-SHOP>>/admin/api/2022-04/orders/<<ORDER-ID>>/fulfillments.json


这是有效载荷:

{
  "fulfillment": {
    "line_items_by_fulfillment_order": [
      {
        "fulfillment_order_id": "5859341008918",
        "fulfillment_order_line_items": [
          {
            "id": "12675478814742",
            "quantity": "3"
          }
        ]
      }
    ],
    "tracking_info": {
      "number": "1234",
      "url": "www.usps.com",
      "company": "Fake Company"
    },
    "notify_customer": false,
    "origin_address": null,
    "message": "test message"
  }
}

相关问题