Anchor to productVariantsBulkCreateproduct
productVariantsBulkCreate
mutation
Requires access scope. Also: The user must have a permission to create product variants.
Creates multiple variants in a single product. This mutation can be called directly or via the bulkOperation.
Anchor to Arguments
Arguments
- Anchor to mediamedia•
List of new media to be added to the product.
- Anchor to productIdproduct•
Id ID!required The ID of the product on which to create the variants.
- Anchor to strategystrategy•Product
Variants Default:DEFAULTBulk Create Strategy The strategy defines which behavior the mutation should observe, such as whether to keep or delete the standalone variant (when product has only a single or default variant) when creating new variants in bulk.
- Anchor to variantsvariants•[Product
Variants requiredBulk Input!]! An array of product variants to be created.
Was this section helpful?
Anchor to ProductVariantsBulkCreatePayload returnsProductVariantsBulkCreatePayload returns
- Anchor to productproduct•
The updated product object.
- Anchor to productVariantsproduct•
Variants The newly created variants.
- Anchor to userErrorsuser•
Errors The list of errors that occurred from executing the mutation.
Was this section helpful?
- Create a new Product Variant
- Create product variants using existing and new option values
- productVariantsBulkCreate reference
Examples
1const { admin } = await authenticate.admin(request);23const response = await admin.graphql(4 `#graphql5 mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {6 productVariantsBulkCreate(productId: $productId, variants: $variants) {7 productVariants {8 id9 title10 selectedOptions {11 name12 value13 }14 }15 userErrors {16 field17 message18 }19 }20 }`,21 {22 variables: {23 "productId": "gid://shopify/Product/20995642",24 "variants": [25 {26 "price": 15.99,27 "compareAtPrice": 19.99,28 "optionValues": [29 {30 "name": "Golden",31 "optionId": "gid://shopify/ProductOption/328272167"32 }33 ]34 }35 ]36 },37 },38);3940const data = await response.json();41
mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
productVariants {
id
title
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}
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 ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { productVariants { id title selectedOptions { name value } } userErrors { field message } } }",
"variables": {
"productId": "gid://shopify/Product/20995642",
"variants": [
{
"price": 15.99,
"compareAtPrice": 19.99,
"optionValues": [
{
"name": "Golden",
"optionId": "gid://shopify/ProductOption/328272167"
}
]
}
]
}
}'
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
productVariants {
id
title
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}`,
{
variables: {
"productId": "gid://shopify/Product/20995642",
"variants": [
{
"price": 15.99,
"compareAtPrice": 19.99,
"optionValues": [
{
"name": "Golden",
"optionId": "gid://shopify/ProductOption/328272167"
}
]
}
]
},
},
);
const data = await response.json();
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
productVariants {
id
title
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}`,
"variables": {
"productId": "gid://shopify/Product/20995642",
"variants": [
{
"price": 15.99,
"compareAtPrice": 19.99,
"optionValues": [
{
"name": "Golden",
"optionId": "gid://shopify/ProductOption/328272167"
}
]
}
]
},
},
});
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 ProductVariantsCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
productVariants {
id
title
selectedOptions {
name
value
}
}
userErrors {
field
message
}
}
}
QUERY
variables = {
"productId": "gid://shopify/Product/20995642",
"variants": [{"price"=>15.99, "compareAtPrice"=>19.99, "optionValues"=>[{"name"=>"Golden", "optionId"=>"gid://shopify/ProductOption/328272167"}]}]
}
response = client.query(query: query, variables: variables)
Input variables
JSON1{2 "productId": "gid://shopify/Product/20995642",3 "variants": [4 {5 "price": 15.99,6 "compareAtPrice": 19.99,7 "optionValues": [8 {9 "name": "Golden",10 "optionId": "gid://shopify/ProductOption/328272167"11 }12 ]13 }14 ]15}
Response
JSON1{2 "productVariantsBulkCreate": {3 "productVariants": [4 {5 "id": "gid://shopify/ProductVariant/1070325177",6 "title": "Golden",7 "selectedOptions": [8 {9 "name": "Title",10 "value": "Golden"11 }12 ]13 }14 ],15 "userErrors": []16 }17}