Anchor to productOptionUpdateproduct
productOptionUpdate
mutation
Requires access scope. Also: The user must have a permission to edit products and manage product variants.
Updates a product option.
Anchor to Arguments
Arguments
- Anchor to optionoption•Option
Update requiredInput! Option to update.
- Anchor to optionValuesToAddoption•
Values To Add New option values to create.
- Anchor to optionValuesToDeleteoption•
Values To Delete IDs of the existing option values to delete.
- Anchor to optionValuesToUpdateoption•
Values To Update Existing option values to update.
- Anchor to productIdproduct•
Id ID!required The ID of the Product the Option belongs to.
- Anchor to variantStrategyvariant•
Strategy 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
will be used.
Was this section helpful?
Anchor to ProductOptionUpdatePayload returnsProductOptionUpdatePayload returns
- Anchor to productproduct•
The product with which the option being updated is associated.
- Anchor to userErrorsuser•
Errors [ProductOption non-nullUpdate User Error!]! The list of errors that occurred from executing the mutation.
Was this section helpful?
- Add 2 new option values and update an existing one
- Replace a value with another
- Trying to add an option value with a name that already exists returns an error
- Update name and position of an option
- Update the values of an option linked to a metafield
- productOptionUpdate reference
Examples
1const { admin } = await authenticate.admin(request);23const response = await admin.graphql(4 `#graphql5 mutation updateOption($productId: ID!, $option: OptionUpdateInput!, $optionValuesToAdd: [OptionValueCreateInput!], $optionValuesToUpdate: [OptionValueUpdateInput!], $optionValuesToDelete: [ID!], $variantStrategy: ProductOptionUpdateVariantStrategy) {6 productOptionUpdate(productId: $productId, option: $option, optionValuesToAdd: $optionValuesToAdd, optionValuesToUpdate: $optionValuesToUpdate, optionValuesToDelete: $optionValuesToDelete, variantStrategy: $variantStrategy) {7 userErrors {8 field9 message10 code11 }12 product {13 id14 options {15 id16 name17 values18 position19 optionValues {20 id21 name22 hasVariants23 }24 }25 variants(first: 5) {26 nodes {27 id28 title29 selectedOptions {30 name31 value32 }33 }34 }35 }36 }37 }`,38 {39 variables: {40 "productId": "gid://shopify/Product/1072481063",41 "option": {42 "id": "gid://shopify/ProductOption/1064576526"43 },44 "optionValuesToAdd": [45 {46 "name": "Yellow"47 },48 {49 "name": "Red"50 }51 ],52 "optionValuesToUpdate": [53 {54 "id": "gid://shopify/ProductOptionValue/1054672275",55 "name": "Purple"56 }57 ]58 },59 },60);6162const data = await response.json();63
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/2024-10/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();
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"
}
]
},
},
});
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)
Input variables
JSON1{2 "productId": "gid://shopify/Product/1072481063",3 "option": {4 "id": "gid://shopify/ProductOption/1064576526"5 },6 "optionValuesToAdd": [7 {8 "name": "Yellow"9 },10 {11 "name": "Red"12 }13 ],14 "optionValuesToUpdate": [15 {16 "id": "gid://shopify/ProductOptionValue/1054672275",17 "name": "Purple"18 }19 ]20}
Response
JSON1{2 "productOptionUpdate": {3 "userErrors": [],4 "product": {5 "id": "gid://shopify/Product/1072481063",6 "options": [7 {8 "id": "gid://shopify/ProductOption/1064576526",9 "name": "Color",10 "values": [11 "Purple"12 ],13 "position": 1,14 "optionValues": [15 {16 "name": "Purple",17 "hasVariants": true18 },19 {20 "name": "Yellow",21 "hasVariants": false22 },23 {24 "name": "Red",25 "hasVariants": false26 }27 ]28 }29 ],30 "variants": {31 "nodes": [32 {33 "id": "gid://shopify/ProductVariant/1070325103",34 "title": "Purple",35 "selectedOptions": [36 {37 "name": "Color",38 "value": "Purple"39 }40 ]41 }42 ]43 }44 }45 }46}