product Variants Bulk Createmutation
mutation
2024-01
Requires write
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.
Arguments
List of new media to be added to the product.
The ID of the product on which to create the variants.
An array of product variants to be created.
Was this section helpful?
ProductVariantsBulkCreatePayload returns
The updated product object.
The newly created variants.
The list of errors that occurred from executing the mutation.
Was this section helpful?
Examples
Hide code
DescriptionCopy
mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } }
curl -X POST \ https://your-development-store.myshopify.com/admin/api/2024-01/graphql.json \ -H 'Content-Type: application/json' \ -H 'X-Shopify-Access-Token: {access_token}' \ -d '{ "query": "mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } }", "variables": { "productId": "gid://shopify/Product/20995642", "variants": [ { "options": [ "Fashionable" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Synthetic Leather" } ] }, { "options": [ "Rugged" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Reinforced Polyethylene" } ] } ] } }'
const { admin } = await authenticate.admin(request); const response = await admin.graphql( `#graphql mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } }`, { variables: { "productId": "gid://shopify/Product/20995642", "variants": [ { "options": [ "Fashionable" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Synthetic Leather" } ] }, { "options": [ "Rugged" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Reinforced Polyethylene" } ] } ] }, }, ); 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 productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } } QUERY variables = { "productId": "gid://shopify/Product/20995642", "variants": [{"options"=>["Fashionable"], "metafields"=>[{"namespace"=>"my_fields", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Synthetic Leather"}]}, {"options"=>["Rugged"], "metafields"=>[{"namespace"=>"my_fields", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Reinforced Polyethylene"}]}] } response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session}); const data = await client.query({ data: { "query": `mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } }`, "variables": { "productId": "gid://shopify/Product/20995642", "variants": [ { "options": [ "Fashionable" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Synthetic Leather" } ] }, { "options": [ "Rugged" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Reinforced Polyethylene" } ] } ] }, }, });
use Shopify\Clients\Graphql; $client = new Graphql("your-development-store.myshopify.com", $accessToken); $query = <<<QUERY mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { product { id } productVariants { id metafields(first: 1) { edges { node { namespace key value } } } } userErrors { field message } } } QUERY; $variables = [ "productId" => "gid://shopify/Product/20995642", "variants" => [{"options"=>["Fashionable"], "metafields"=>[{"namespace"=>"my_fields", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Synthetic Leather"}]}, {"options"=>["Rugged"], "metafields"=>[{"namespace"=>"my_fields", "key"=>"liner_material", "type"=>"single_line_text_field", "value"=>"Reinforced Polyethylene"}]}], ]; $response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{ "productId": "gid://shopify/Product/20995642", "variants": [ { "options": [ "Fashionable" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Synthetic Leather" } ] }, { "options": [ "Rugged" ], "metafields": [ { "namespace": "my_fields", "key": "liner_material", "type": "single_line_text_field", "value": "Reinforced Polyethylene" } ] } ] }
Hide code
Response
JSON
{ "productVariantsBulkCreate": { "product": { "id": "gid://shopify/Product/20995642" }, "productVariants": [ { "id": "gid://shopify/ProductVariant/1070325089", "metafields": { "edges": [ { "node": { "namespace": "my_fields", "key": "liner_material", "value": "Synthetic Leather" } } ] } }, { "id": "gid://shopify/ProductVariant/1070325090", "metafields": { "edges": [ { "node": { "namespace": "my_fields", "key": "liner_material", "value": "Reinforced Polyethylene" } } ] } } ], "userErrors": [] } }