A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
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...
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;
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.