skip to Main Content

I’ve been trying to create product with variants using Python and GQL,

but lacking something and dosen’t work as I want.

it says "Field ‘optionValues’ doesn’t exist on type ‘ProductOption’" but I reffer to a official document and copy it

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)

response

{
  "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"
      }
    }
  ]
}

Im excepting to register a prodcut with variants

2

Answers


  1. The way you’re able to use options to create variants is like this:

    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"]
          }
        ]
      }
    }
    
    Login or Signup to reply.
  2. Error occurred while creating product variants in bulk: RequestError: Variable $variantsInput of type [ProductVariantsBulkInput!]! was provided invalid value for 0.title (Field is not defined on ProductVariantsBulkInput), 0.variants (Field is not defined on ProductVariantsBulkInput)

    it seems they moved/deprecated title and variants as used there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search