shopifyshopify-api

Shopify API from PHP: problem with linked metafields in a productCreate call


I'm trying to create a productCreate mutation which specifies the available colours for the product, but I'm getting the error 'The 'shopify.color-pattern' metafield has no values and none were passed in for this linked option'.

I'm passing in variables as follows (closing brackets elided) which are then json-encoded.

[variables] => stdClass Object
(
[product] => stdClass Object
(
[title] => Herring Sittaford Chelsea boots
[productOptions] => Array
(
[0] => stdClass Object
(
[name] => Color
[position] => 1
[linkedMetafield] => stdClass Object
(
[key] => color-pattern
[namespace] => shopify
)

[values] => Array
(
[0] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330016585
[name] => Caramel Suede
)

[1] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330049353
[name] => Honey Suede
)

[2] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330114889
[name] => Ocean Suede
)

[3] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330606409
[name] => Olive Suede
)

[4] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330639177
[name] => Sand Suede
)

[5] => stdClass Object
(
[linkedMetafieldValue] => gid://shopify/Metaobject/169330671945
[name] => Wheat Nubuck
)

The shopify dox are not helpful on this. I've tried various tweaks on this and nothing helps. Pass in values as an array on the linkedMetafieldobject and I get 'Cannot specify 'values' for an option linked to a metafield'.

This works if I don't use linked metafields, but I want this to be nailed down to the defined colours we've set up programmatically, I don't want duplicates.

Anyone got any suggestions?

Thanks in advance,

G


Solution

  • first of all this has nothing to do with php

    you are just using the wrong mutation for your scope

    as you can read in the official documentation:

    https://shopify.dev/docs/api/admin-graphql/latest/mutations/productcreate

    The productCreate mutation only supports creating a product with its initial product variant.

    your best bet is replace productCreate with productSet

    this are two good starting point:

    https://shopify.dev/docs/apps/build/graphql/migrate/new-product-model/metafield-linked

    https://shopify.dev/docs/api/admin-graphql/latest/mutations/productset

    
        mutation productSet {
          productSet(synchronous: true, input: {
            title: "Herring Sittaford Chelsea boots"
            productOptions: [
              {
                name: "Color",
                linkedMetafield: {
                  namespace: "shopify",
                  key: "color-pattern",
                }
              }
            ],
            variants: [
              {
                position: 0
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330016585" }
                ]
              },
              {
                position: 1
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330049353" }
                ]
              },
              {
                position: 2
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330114889" }
                ]
              },
              {
                position: 3
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330606409" }
                ]
              }
              ,
              {
                position: 4
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330639177" }
                ]
              },
              {
                position: 5
                optionValues: [
                  { optionName: "Color", linkedMetafieldValue: "gid://shopify/Metaobject/169330671945" }
                ]
              }
            ]
          }) {
            userErrors { message }
            product {
              id
            }
          }
        }
    

    or if you want stick on productCreate you could create a product with the first default variant in this way:

        mutation productCreate {
          productCreate(input: {
            title: "Herring Sittaford Chelsea boots",
            productOptions: [
              { 
                name: "Color",
                linkedMetafield: {
                  namespace: "shopify",
                  key: "color-pattern",
                  values: [
                    "gid://shopify/Metaobject/169330016585",
                    "gid://shopify/Metaobject/169330049353",
                    "gid://shopify/Metaobject/169330114889",
                    "gid://shopify/Metaobject/169330606409",
                    "gid://shopify/Metaobject/169330639177",
                    "gid://shopify/Metaobject/169330671945"
                  ]
                }
              }
            ]
          }), {
            userErrors { message }
            product {
              id
              metafields(first: 10) {
                nodes {
                  namespace
                  key
                  value
                }
              }
              options {
                name
                optionValues {
                  name
                  linkedMetafieldValue
                }
              }
            }
          }
        }