Create Product with mutation CreateProductWithOptions

Topic summary

A developer is encountering issues creating a Shopify product with multiple option values using the GraphQL productCreate mutation. Despite following the official documentation example for creating a product with Color (Red, Green) and Size (Small, Medium) options, only the first value of each option appears in the result.

Key Technical Details:

  • The mutation uses productOptions field with nested values arrays
  • PHP/cURL implementation is being used to send the GraphQL request
  • Reference: Shopify Admin GraphQL API documentation for product creation with options

Attempted Solutions:

  • One participant suggested verifying the query structure and provided revised PHP code, emphasizing correct formatting of the productOptions field
  • However, this approach returned validation errors indicating “options” is not a valid field name

Current Understanding:

  • The field name should be productOptions (not options) according to Shopify’s API documentation
  • One response suggests this may be default Shopify behavior: the product query only returns the first option value initially
  • Proposed resolution: Explicitly create variants using productVariantCreate mutation after product creation, with the hasVariants field set to true, to display all option combinations in the UI

Status: The discussion identifies a potential workflow solution but lacks confirmation of successful implementation.

Summarized with AI on November 6. AI used: claude-sonnet-4-5-20250929.

Hi!

I have a problem with new product model. I want to create a example product with graphql

my referance: https://shopify.dev/docs/api/admin-graphql/unstable/mutations/productCreate?example=Create+a+product+with+product+options+and+option+values&language=cURL#argument-input

As you can see in this screenshot, this product have Color and Size options and there are 2 values for each option.

When I use this example,

Result:

It generates only first values. I tried lots of methods but I could not solve it.

My php code;

        $accessToken = $shopify->get_token();
        $shopUrl = 'https://' . $shopify->get_shop() . '/admin/api/unstable/graphql.json';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $shopUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'X-Shopify-Access-Token: '.$accessToken,
        ]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\"query\": \"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 } } } } } }\",\n \"variables\": {\n    \"input\": {\n      \"title\": \"New product\",\n      \"productOptions\": [\n        {\n          \"name\": \"Color\",\n          \"values\": [\n            {\n              \"name\": \"Red\"\n            },\n            {\n              \"name\": \"Green\"\n            }\n          ]\n        },\n        {\n          \"name\": \"Size\",\n          \"values\": [\n            {\n              \"name\": \"Small\"\n            },\n            {\n              \"name\": \"Medium\"\n            }\n          ]\n        }\n      ]\n    }\n  }\n}");

        $response = curl_exec($ch);

        curl_close($ch);

Check Your Query: It seems like your GraphQL query might be missing some details or incorrectly structured. Make sure you’re correctly specifying the options and their values.

Correct Example Code: Here’s a revised version of your mutation query that should correctly include multiple values for each option:

$query = 'mutation CreateProductWithOptions($input: ProductInput!) {
productCreate(input: $input) {
userErrors {
field
message
}
product {
id
options {
id
name
position
values
}
variants(first: 5) {
nodes {
id
title
selectedOptions {
name
value
}
}
}
}
}
}';

$variables = [
'input' => [
'title' => 'New product',
'options' => [
[
'name' => 'Color',
'values' => ['Red', 'Green']
],
[
'name' => 'Size',
'values' => ['Small', 'Medium']
]
]
]
];

$postFields = json_encode([
'query' => $query,
'variables' => $variables
]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $shopUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Shopify-Access-Token: ' . $accessToken,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

Make sure you adjust “productOptions” to “options” in the query and ensure the option values are correctly formatted.

Validate Your Input: Double-check the format and content of the options and values you’re sending. They should be in the format that Shopify expects.

By making these adjustments, you should be able to include all your option values for the new product. If you still face issues, you might want to check Shopify’s API documentation or reach out to their support for further assistance.

Hi,

Thanks for your effort but your code is not working.

Error response;

J8DFEYZ.png

Also check this please;

https://shopify.dev/docs/api/admin-graphql/unstable/input-objects/ProductInput#field-productoptions

“options” is not valid.

Valid value is “productOptions”

HI ,there

This is the default behavior outcome in Shopify. When you utilize the product query of Graphql, you will observe that in your result, only the first value of options has the Variants field set to true. You need to explicitly use productVariantCreate to create the variants, and then the results will be displayed in the UI.