Shopify GraphQL:如何使用 Python 和 GraphQL 添加变体

问题描述 投票:0回答:1

我一直在尝试使用 Python 和 GQL 创建具有变体的产品,

但缺少一些东西并且不能按我想要的方式工作。

它说“‘ProductOption’类型中不存在字段‘optionValues’”但我参考了官方文档并复制了它

https://shopify.dev/docs/api/admin-graphql/2023-10/mutations/productcreate#examples-Create_a_product_with_options_and_option_values

import requests
import json

# Shopify API credentials
shop_url = "ShopURL"
access_token = "access_token"

# GraphQL endpoint
graphql_url = f"{shop_url}/admin/api/2023-10/graphql.json"

# Build the GraphQL mutation to create a product with options and option values
mutation = """
mutation CreateProductWithOptions($input: ProductInput!) {
  productCreate(input: $input) {
    userErrors {
      field
      message
    }
    product {
      id
      options {
        id
        name
        position
        values
        optionValues {
          id
          name
          hasVariants
        }
      }
      variants(first: 5) {
        nodes {
          id
          title
          selectedOptions {
            name
            value
          }
        }
      }
    }
  }
}


"""

headers = {
    "Content-Type": "application/json",
    "X-Shopify-Access-Token": access_token,
}

# Define the variables with the product input
variables = {
  "input": {
    "title": "New product",
    "optionValues": [
      {
        "name": "Color",
        "values": [
          {
            "name": "Red"
          },
          {
            "name": "Green"
          }
        ]
      },
      {
        "name": "Size",
        "values": [
          {
            "name": "Small"
          },
          {
            "name": "Medium"
          }
        ]
      }
    ]
  }
}



# Create the request data
data = {
    "query": mutation,
    "variables": variables
}

response = requests.post(graphql_url, json=data, headers=headers)

if response.status_code == 200:
    result = response.json()
    print("Product with options and option values created:")
    print(json.dumps(result, indent=2))
else:
    print(f"Failed to create the product. Status Code: {response.status_code}")
    print(response.text)

回应

{
  "errors": [
    {
      "message": "Field 'optionValues' doesn't exist on type 'ProductOption'",
      "locations": [
        {
          "line": 15,
          "column": 9
        }
      ],
      "path": [
        "mutation CreateProductWithOptions",
        "productCreate",
        "product",
        "options",
        "optionValues"
      ],
      "extensions": {
        "code": "undefinedField",
        "typeName": "ProductOption",
        "fieldName": "optionValues"
      }
    }
  ]
}

我不注册带有变体的产品

python graphql shopify gql
1个回答
0
投票

使用选项创建变体的方式如下:

mutation CreateProductWithOptions($input: ProductInput!) {
  productCreate(input: $input) {
    product {
      id
      options {
        name
        values
        id
        position
      }
      variants(first: 5) {
        nodes {
          id
          title
          selectedOptions {
            name
            value
          }
        }
      }
    }
    userErrors {
      message
      field
    }
  }
}
{
  "input": {
    "options": ["Size", "Color"],
    "title": "New Product",
    "variants": [
      {
        "options": ["Small", "Red"]
      },
      {
        "options": ["Small", "Green"]
      },
      {
        "options": ["Medium", "Green"]
      },
      {
        "options": ["Medium", "Red"]
      }
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.