productOptionsCreate and productCreate create only one value for multiple option values

Here is request example:

{
    "query": "mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!, $variantStrategy: ProductOptionCreateVariantStrategy) { productOptionsCreate(productId: $productId, options: $options, variantStrategy: $variantStrategy) { userErrors { field message code } product { id variants(first: 10) { nodes { id title selectedOptions { name value } } } options { id name values position optionValues { id name hasVariants } } } } }",
    "variables": {
        "productId": "gid://shopify/Product/8779717902498",
        "options": [
            {
                "name": "Choose Color",
                "position": 1,
                "values": [
                    {
                        "name": "Blue"
                    },
                    {
                        "name": "Red"
                    },
                    {
                        "name": "Green"
                    }
                ]
            }
        ]
    }
}

I visit admin and it only create the “Blue” one. same for productCreate

1 Like

Hi @Juno_kyo,

This is actually expected behaviour here, and you’ll see that the Option Values are actually being created and do exist, but an actual Product Variant is only being created for the first option value.

We can see this with the response of the query you shared above, I ran this on my test store to confirm:

    "data": {
        "productOptionsCreate": {
            "userErrors": [],
            "product": {
                "id": "gid://shopify/Product/8778610573474",
                "variants": {
                    "nodes": [
                        {
                            "id": "gid://shopify/ProductVariant/45849086820514",
                            "title": "Blue",
                            "selectedOptions": [
                                {
                                    "name": "Choose Color",
                                    "value": "Blue"
                                }
                            ]
                        }
                    ]
                },
                "options": [
                    {
                        "id": "gid://shopify/ProductOption/11200055640226",
                        "name": "Choose Color",
                        "values": [
                            "Blue"
                        ],
                        "position": 1,
                        "optionValues": [
                            {
                                "id": "gid://shopify/ProductOptionValue/4514145829026",
                                "name": "Blue",
                                "hasVariants": true
                            },
                            {
                                "id": "gid://shopify/ProductOptionValue/4514145861794",
                                "name": "Red",
                                "hasVariants": false
                            },
                            {
                                "id": "gid://shopify/ProductOptionValue/4514145894562",
                                "name": "Green",
                                "hasVariants": false
                            }
                        ]
                    }
                ]
            }
        }
    }

We can see here that the Red and Green option values are created, but an actual Product Variant is only created for the Blue option value.

If you wish to create Product Variants for each new Product Option Value, and not only the first one listed, you will need to pass the "variantStrategy": "CREATE", argument in the mutation input, as this tells the mutation to create new variants for all new option values.

If you do not pass the variantStrategy argument with the mutation, it defaults to LEAVE_AS_IS which will only create a new Product Variant for the first listed Option Value created.

This behaviour is described in the Shopify.dev documentation for the productOptionsCreate mutation as well.

Here is an example of the variables I used to do this correctly:

{
    "productId": "gid://shopify/Product/8778609492130",
    "variantStrategy": "CREATE",
    "options": [
        {
            "name": "Choose Color",
            "position": 1,
            "values": [
                {
                    "name": "Blue"
                },
                {
                    "name": "Red"
                },
                {
                    "name": "Green"
                }
            ]
        }
    ]
}

so what about productCreate? productCreate also create only first option?
Can you show me how?

**productOptionsCreate is ok. So I have to call API 2 times

  1. Create product
  2. Create Option
  3. Update variant.

Any simple way?**

Hi @Juno_kyo,

The productCreate mutation is similar in that it only creates a single initial variant, though you can create multiple Product Options and Option Values with the call.

If you need to create more variants after the first variant with productCreate, you can do the following:

  1. productCreate - create product with multiple options and option values, this will create a single variant to start.
  2. productVariantsBulkCreate - create the remaining variants, using the product options you created in the previous step.

Alternatively you can use the singular mutation, productSet, to create the full product with all options and variants, in a single api call.

Typically if your app is creating products via incremental input from users you’ll want to use the productCreate/productOptionsCreate and productVariantsBulkCreate workflow in creating your products and variants. And if your app is importing products from an external database with all of the product data available immediately, it’s recommended to use the productSet workflow.

Here’s some additional documentation on the new Product Model API and the various workflows described above for more info:

And here’s the reference docs for the mutations mentioned above: