Anchor to section titled 'undefined'

productOptionUpdate
mutation

Requires write_products access scope. Also: The user must have a permission to edit products and manage product variants.

Updates a product option.


Option to update.

New option values to create.

IDs of the existing option values to delete.

Existing option values to update.

Anchor to productId
productId
required

The ID of the Product the Option belongs to.

The strategy defines which behavior the mutation should observe regarding variants, such as creating variants or deleting them in response to option values to add or to delete. If not provided or set to null, the strategy LEAVE_AS_IS will be used.


Was this section helpful?

The product with which the option being updated is associated.

The list of errors that occurred from executing the mutation.


Was this section helpful?

Examples

Hide code
DescriptionCopy
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
        values
        position
        optionValues {
          id
          name
          hasVariants
        }
      }
      variants(first: 5) {
        nodes {
          id
          title
          selectedOptions {
            name
            value
          }
        }
      }
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/unstable/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "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 values position optionValues { id name hasVariants } } variants(first: 5) { nodes { id title selectedOptions { name value } } } } } }",
 "variables": {
    "productId": "gid://shopify/Product/1072481063",
    "option": {
      "id": "gid://shopify/ProductOption/1064576526"
    },
    "optionValuesToAdd": [
      {
        "name": "Yellow"
      },
      {
        "name": "Red"
      }
    ],
    "optionValuesToUpdate": [
      {
        "id": "gid://shopify/ProductOptionValue/1054672275",
        "name": "Purple"
      }
    ]
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  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
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
        variants(first: 5) {
          nodes {
            id
            title
            selectedOptions {
              name
              value
            }
          }
        }
      }
    }
  }`,
  {
    variables: {
      "productId": "gid://shopify/Product/1072481063",
      "option": {
        "id": "gid://shopify/ProductOption/1064576526"
      },
      "optionValuesToAdd": [
        {
          "name": "Yellow"
        },
        {
          "name": "Red"
        }
      ],
      "optionValuesToUpdate": [
        {
          "id": "gid://shopify/ProductOptionValue/1054672275",
          "name": "Purple"
        }
      ]
    },
  },
);

const data = await response.json();
session = ShopifyAPI::Auth::Session.new(
  shop: "your-development-store.myshopify.com",
  access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
  session: session
)

query = <<~QUERY
  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
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
        variants(first: 5) {
          nodes {
            id
            title
            selectedOptions {
              name
              value
            }
          }
        }
      }
    }
  }
QUERY

variables = {
  "productId": "gid://shopify/Product/1072481063",
  "option": {
    "id": "gid://shopify/ProductOption/1064576526"
  },
  "optionValuesToAdd": [{"name"=>"Yellow"}, {"name"=>"Red"}],
  "optionValuesToUpdate": [{"id"=>"gid://shopify/ProductOptionValue/1054672275", "name"=>"Purple"}]
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `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
            values
            position
            optionValues {
              id
              name
              hasVariants
            }
          }
          variants(first: 5) {
            nodes {
              id
              title
              selectedOptions {
                name
                value
              }
            }
          }
        }
      }
    }`,
    "variables": {
      "productId": "gid://shopify/Product/1072481063",
      "option": {
        "id": "gid://shopify/ProductOption/1064576526"
      },
      "optionValuesToAdd": [
        {
          "name": "Yellow"
        },
        {
          "name": "Red"
        }
      ],
      "optionValuesToUpdate": [
        {
          "id": "gid://shopify/ProductOptionValue/1054672275",
          "name": "Purple"
        }
      ]
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  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
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
        variants(first: 5) {
          nodes {
            id
            title
            selectedOptions {
              name
              value
            }
          }
        }
      }
    }
  }
QUERY;

$variables = [
  "productId" => "gid://shopify/Product/1072481063",
  "option" => [
    "id" => "gid://shopify/ProductOption/1064576526",
  ],
  "optionValuesToAdd" => [{"name"=>"Yellow"}, {"name"=>"Red"}],
  "optionValuesToUpdate" => [{"id"=>"gid://shopify/ProductOptionValue/1054672275", "name"=>"Purple"}],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "productId": "gid://shopify/Product/1072481063",
  "option": {
    "id": "gid://shopify/ProductOption/1064576526"
  },
  "optionValuesToAdd": [
    {
      "name": "Yellow"
    },
    {
      "name": "Red"
    }
  ],
  "optionValuesToUpdate": [
    {
      "id": "gid://shopify/ProductOptionValue/1054672275",
      "name": "Purple"
    }
  ]
}
Hide code
Response
JSON
{
  "productOptionUpdate": {
    "userErrors": [],
    "product": {
      "id": "gid://shopify/Product/1072481063",
      "options": [
        {
          "id": "gid://shopify/ProductOption/1064576526",
          "name": "Color",
          "values": [
            "Purple"
          ],
          "position": 1,
          "optionValues": [
            {
              "name": "Purple",
              "hasVariants": true
            },
            {
              "name": "Yellow",
              "hasVariants": false
            },
            {
              "name": "Red",
              "hasVariants": false
            }
          ]
        }
      ],
      "variants": {
        "nodes": [
          {
            "id": "gid://shopify/ProductVariant/1070325103",
            "title": "Purple",
            "selectedOptions": [
              {
                "name": "Color",
                "value": "Purple"
              }
            ]
          }
        ]
      }
    }
  }
}