GraphQL Proper way to create variants/options?

What is the right way to create options/variants with graphql? I’m struggling to find the right structure.

Right now I am doing the following:

mutation {
  productCreate(input: {handle: "'.$handle.'", title: "'.$title.'", options: ['Color','Oriention']}) {
    product {
      id
    }
  }
}

This created a variant called “default title” which is not ideal.

If I just don’t create options and then create variants next after the product like so:

$query = 
            'mutation productVariantCreate($input: ProductVariantInput!) {
                productVariantCreate(input: $input) {
                    product {
                        id
                    }
                    productVariant {
                        id
                    }
                    userErrors {
                        field
                        message
                    }
                }
            }';
            $variables = [
                "input" => [
                    "price" => $price,
                    "productId" => $product_gid,
                    "options" => $options,
                    "title" => $title,
                ],
            ];

Here $options take the form [“Red”, “Left Side”], I have tried several other combinations like [{“orientation”: “Left Side”},{“color”: “Red”}] and several others but I cannot get it to work how I want.

If anyone has a sample graphql query that shows how to make product variants/options correctly please let me know.

1 Like

I had the same issue with the sample payload guide https://shopify.dev/docs/api/admin-graphql/2024-01/mutations/productCreate#examples-Create_a_product_with_product_options_and_option_values

{"input": {
    "title": "New product",
    "productOptions": [
      {
        "name": "Color",
        "values": [
          { "name": "Red" }, 
          { "name": "Green" }
        ]
      },
      {
        "name": "Size",
        "values": [
          { "name": "Small" }, 
          { "name": "Medium" }
        ]
      }
    ]  }}

There is no tag productOptions for the ProductInput.

“errors”: “message”: “Variable $input of type ProductInput! was provided invalid value for productOptions (Field is not defined on ProductInput)”,

It was still error if I changed the tag productOptions to options
“problems”: “Could not coerce value {name:"Color",values:[{name:"Red"},{name:"Green"}]} to String”

Not sure what is the right sample payload for

“Create a product with product options and option values
https://shopify.dev/docs/api/admin-graphql/2024-01/mutations/productCreate#examples-Create_a_product_with_product_options_and_option_values

Can someone in the Shopify Team have a look at the document and fix the issue?

Thank you,

Manh

It’s very unlikely you will ever get a useful response from the Shopify Team.

You could try the Rest Api and see if you have more luck there https://shopify.dev/docs/api/admin-rest/2024-01/resources/product#post-products though you won’t have the bulk import abilities and other functionality.

I’ve tried with this payload example, and it works :

const input = {
title: 'New product',
options: ['Size', 'Color'],
variants: [
{
price: 0,
options: ['Large', 'red']
},
{
price: 0,
options: ['Medium', 'green']
},
{
price: 0,
options: ['Large', 'green']
},
{
price: 0,
options: ['Medium', 'red']
}
] };

The only think is that you have to create all the variants with a value for each options

Hope it helps

Best way I have found is:

  1. Create product with mutation createProductWithOptions . In this mutation, you will provide all possible options under productOptions. You will get variant id also with productid.

  2. Once Product is created, you can use productVariantsBulkUpdate or productVariantsBulkCreate mutation to give all variants details. To update variant detail of variant created while product creation, use productVariantsBulkUpdate or you are creating new variant useproductVariantsBulkCreate

1 Like