我正在 Shopify 上创建一个自定义应用程序并尝试创建草稿订单。当我创建草稿订单时,出现以下错误。
{
"errors": {
"base": [
"The merchandise variant referenced by this term condition could not be found."
]
}
}
我写的代码如下:
# Create a Shopify session
access_token = '<ACCESS_TOKEN>'
shop_url = "<URL>"
# session = shopify.Session(cart.storeId, GLOBAL_SHOPIFY_API_VERSION, access_token)
# shopify.ShopifyResource.activate_session(session)
# # Create a new draft order
# new_draft_order = shopify.DraftOrder()
# new_draft_order.line_items = {
# "variant_id": cart.cartItem.get("variantId"),
# "quantity": cart.cartItem.get("quantity")
# }
# await new_draft_order.save()
# Set the headers and the data for the request
headers = {
'X-Shopify-Access-Token': access_token,
'Content-Type': 'application/json',
}
json_data = {
'draft_order': {
'line_items': [
{
'varianct_id': cart.cartItem.get("variantId"),
'quantity': cart.cartItem.get("quantity"),
},
],
},
}
response = requests.post(
f'https://{shop_url}/admin/api/{GLOBAL_SHOPIFY_API_VERSION}/draft_orders.json',
headers=headers,
json=json_data,
)
# Check if the request was successful
if response.status_code == 200:
return response.json()
if response.status_code == 202:
return response.json()
return response.json()
我已使用端点的 API 访问令牌,因此它应该可以工作。变体ID也是正确的,可以在shopify平台上查看。
Python API 无论如何都是不正确的,并且重定向到错误的 url,但我不知道为什么请求也没有通过。
您可能想检查变体 ID。您使用的是 GID 还是只是简单的 Shopify 旧资源之一?
"gid://shopify/Product/10079785100"
versus just 10079785100
可能是这样...