Link option to metafield

Hello,

I want to link existing product options to a metafield.

Manually it would look like this:

  1. Select “connect metafield” in the color option

  2. Choose “color”

It would then look like this:

It pulled the data from my pre-defined color product category metafield:

I’m currently trying to reproduce the linking of the color metafield to the option but I cant get it to work.

What I tried so far:

Use an admin API request > productOptionUpdate

Mutation input:

{
“productId”: “gid://shopify/Product/8582628278424”,
“option”:
{
“linkedMetafield”: {
“key”: “color-pattern”,
“namespace”: “shopify”
},
“id”: “gid://shopify/ProductOption/10830831321240”,
“name”: “Color”,
“position”: 1
},
“optionValuesToUpdate”: [
{
“id”: “gid://shopify/ProductOptionValue/4298614374552”,
“name”: “Charcoal”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278357656”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614243480”,
“name”: “White”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278259352”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614276248”,
“name”: “Black”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278226584”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614341784”,
“name”: “Military Green”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278324888”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614407320”,
“name”: “Heather Red”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278292120”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614309016”,
“name”: “Natural”,
“linkedMetafieldValue”: “gid://shopify/Metaobject/73278193816”
}
]
}

What is wrong with this input? There is currently no metafield linked, but I get this error message in flow:

“Mutation had user errors: “An option cannot have both metafield linked and nonlinked option values.””

This is how it looks right now:

“options”: [
{
“linkedMetafield”: null,
“id”: “gid://shopify/ProductOption/10830831321240”,
“name”: “Color”,
“optionValues”: [
{
“id”: “gid://shopify/ProductOptionValue/4298614374552”,
“linkedMetafieldValue”: null,
“name”: “Charcoal”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614243480”,
“linkedMetafieldValue”: null,
“name”: “White”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614276248”,
“linkedMetafieldValue”: null,
“name”: “Black”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614341784”,
“linkedMetafieldValue”: null,
“name”: “Military Green”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614407320”,
“linkedMetafieldValue”: null,
“name”: “Heather Red”
},
{
“id”: “gid://shopify/ProductOptionValue/4298614309016”,
“linkedMetafieldValue”: null,
“name”: “Natural”
}
]
}

Not really a Flow question but an API question. Taking the error message at it’s word, it looks like you cannot mix metafield-defined options with those created in the Admin. So you would need to remove the existing options first I think. Then add them back as metafields.

Hello,

thank you.

I actually figured out a way over csv editing…

Dear, Summmitsoul

I have the same problem as you.

I would appreciate it if you could also tell me the solution.

Tal vez es un poco tarde pero tuve el mismo mensaje de error.

Al pasar linkedMetafieldValue en las options debes evitar pasar el campo name porque el valor name se tomará del metaobject referenciado ya que se vincularán.

Documentación oficial.
https://shopify.dev/docs/api/admin-graphql/latest/mutations/productoptionupdate?example=update-the-values-of-an-option-linked-to-a-metafield

Este un ejemplo de la mutación en GQL

mutation updateOption($productId: ID!, $option: OptionUpdateInput!, $optionValuesToAdd: [OptionValueCreateInput!], $optionValuesToUpdate: [OptionValueUpdateInput!], $optionValuesToDelete: [ID!], $variantStrategy: ProductOptionUpdateVariantStrategy) {
  productOptionUpdate(
    productId: $productId
    option: $option
    optionValuesToAdd: $optionValuesToAdd
    optionValuesToUpdate: $optionValuesToUpdate
    optionValuesToDelete: $optionValuesToDelete
    variantStrategy: $variantStrategy
  ) {
    userErrors {
      field
      message
      code
    }
    product {
      id
      options {
        id
        name
        optionValues {
          id
          name
          linkedMetafieldValue
        }
      }
    }
  }
}

y en las variables que pasamos, evitamos pasar el campo name en las options.

{
  "productId": "gid://shopify/Product/9988780884268",
  "option": {
    "id": "gid://shopify/ProductOption/12466553815340",
    "name": "Color",
    "linkedMetafield": {
      "key": "color-pattern",
      "namespace": "shopify"
    }
  },
  "optionValuesToUpdate": [
    {
      "id": "gid://shopify/ProductOptionValue/5080685281580",
      "linkedMetafieldValue": "gid://shopify/Metaobject/135817363756"
    },
    {
      "id": "gid://shopify/ProductOptionValue/5080685314348",
      "linkedMetafieldValue": "gid://shopify/Metaobject/135817396524"
    },
    {
      "id": "gid://shopify/ProductOptionValue/5080685347116",
      "linkedMetafieldValue": "gid://shopify/Metaobject/135817330988"
    }
  ]
}

Recuerda que todas las opciones deben ser actualizadas. Si son 5 option values, las 5 deben ser actualizadas.

El resultado debería verse similar a esto:

{
  "data": {
    "productOptionUpdate": {
      "userErrors": [],
      "product": {
        "id": "gid://shopify/Product/9988780884268",
        "options": [
          {
            "id": "gid://shopify/ProductOption/12466553815340",
            "name": "Color",
            "optionValues": [
              {
                "id": "gid://shopify/ProductOptionValue/5080685281580",
                "name": "Beige",
                "linkedMetafieldValue": "gid://shopify/Metaobject/135817363756"
              },
              {
                "id": "gid://shopify/ProductOptionValue/5080685314348",
                "name": "Black",
                "linkedMetafieldValue": "gid://shopify/Metaobject/135817396524"
              },
              {
                "id": "gid://shopify/ProductOptionValue/5080685347116",
                "name": "White",
                "linkedMetafieldValue": "gid://shopify/Metaobject/135817330988"
              }
            ]
          },
          {
            "id": "gid://shopify/ProductOption/12466553848108",
            "name": "Size",
            "optionValues": [
              {
                "id": "gid://shopify/ProductOptionValue/5080685478188",
                "name": "5.5",
                "linkedMetafieldValue": null
              },
              ...
            ]
          }
        ]
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 12,
      "actualQueryCost": 12,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1988,
        "restoreRate": 100
      }
    }
  }
}

Okay, this approach makes some sense: create the product option value for a given metaobject independently of the variant, and then use the product option value ID(s) to create the variant. I’ll look into that. Thanks for the suggestion!