Anchor to productOptionsCreateproduct
productOptionsCreate
mutation
Requires access scope. Also: The user must have a permission to create product variants.
Creates options on a product.
Anchor to Arguments
Arguments
- Anchor to optionsoptions•[Option
Create requiredInput!]! Options to add to the product.
- Anchor to productIdproduct•
Id ID!required The ID of the product to update.
Was this section helpful?
Anchor to ProductOptionsCreatePayload returnsProductOptionsCreatePayload returns
- Anchor to productproduct•
The updated product object.
- Anchor to userErrorsuser•
Errors The list of errors that occurred from executing the mutation.
Was this section helpful?
- Create a product option linked to a metafield
- Create a product option linked to a metafield (with existing values)
- productOptionsCreate reference
Examples
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
productOptionsCreate(productId: $productId, options: $options) {
userErrors {
field
message
code
}
product {
options {
name
linkedMetafield {
namespace
key
}
optionValues {
name
linkedMetafieldValue
}
}
}
}
}`,
{
variables: {
"productId": "gid://shopify/Product/1072481128",
mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
productOptionsCreate(productId: $productId, options: $options) {
userErrors {
field
message
code
}
product {
options {
name
linkedMetafield {
namespace
key
}
optionValues {
name
linkedMetafieldValue
}
}
}
}
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) { productOptionsCreate(productId: $productId, options: $options) { userErrors { field message code } product { options { name linkedMetafield { namespace key } optionValues { name linkedMetafieldValue } } } } }",
"variables": {
"productId": "gid://shopify/Product/1072481128",
"options": [
{
"name": "Color",
"linkedMetafield": {
"namespace": "shopify",
"key": "color-pattern",
"values": [
"gid://shopify/Metaobject/971662503",
"gid://shopify/Metaobject/971662504",
"gid://shopify/Metaobject/971662505"
]
}
}
]
}
}'
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
productOptionsCreate(productId: $productId, options: $options) {
userErrors {
field
message
code
}
product {
options {
name
linkedMetafield {
namespace
key
}
optionValues {
name
linkedMetafieldValue
}
}
}
}
}`,
{
variables: {
"productId": "gid://shopify/Product/1072481128",
"options": [
{
"name": "Color",
"linkedMetafield": {
"namespace": "shopify",
"key": "color-pattern",
"values": [
"gid://shopify/Metaobject/971662503",
"gid://shopify/Metaobject/971662504",
"gid://shopify/Metaobject/971662505"
]
}
}
]
},
},
);
const data = await response.json();
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
productOptionsCreate(productId: $productId, options: $options) {
userErrors {
field
message
code
}
product {
options {
name
linkedMetafield {
namespace
key
}
optionValues {
name
linkedMetafieldValue
}
}
}
}
}`,
"variables": {
"productId": "gid://shopify/Product/1072481128",
"options": [
{
"name": "Color",
"linkedMetafield": {
"namespace": "shopify",
"key": "color-pattern",
"values": [
"gid://shopify/Metaobject/971662503",
"gid://shopify/Metaobject/971662504",
"gid://shopify/Metaobject/971662505"
]
}
}
]
},
},
});
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 createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
productOptionsCreate(productId: $productId, options: $options) {
userErrors {
field
message
code
}
product {
options {
name
linkedMetafield {
namespace
key
}
optionValues {
name
linkedMetafieldValue
}
}
}
}
}
QUERY
variables = {
"productId": "gid://shopify/Product/1072481128",
"options": [{"name"=>"Color", "linkedMetafield"=>{"namespace"=>"shopify", "key"=>"color-pattern", "values"=>["gid://shopify/Metaobject/971662503", "gid://shopify/Metaobject/971662504", "gid://shopify/Metaobject/971662505"]}}]
}
response = client.query(query: query, variables: variables)
Input variables
JSON{
"productId": "gid://shopify/Product/1072481128",
"options": [
{
"name": "Color",
"linkedMetafield": {
"namespace": "shopify",
"key": "color-pattern",
"values": [
"gid://shopify/Metaobject/971662503",
"gid://shopify/Metaobject/971662504",
"gid://shopify/Metaobject/971662505"
]
}
}
]
}
Response
JSON{
"productOptionsCreate": {
"userErrors": [],
"product": {
"options": [
{
"name": "Color",
"linkedMetafield": {
"namespace": "shopify",
"key": "color-pattern"
},
"optionValues": [
{
"name": "Red",
"linkedMetafieldValue": "gid://shopify/Metaobject/971662503"
},
{
"name": "Blue",
"linkedMetafieldValue": "gid://shopify/Metaobject/971662504"
},
{
"name": "Yellow",
"linkedMetafieldValue": "gid://shopify/Metaobject/971662505"