Anchor to section titled 'undefined'

productVariantsBulkCreate
mutation

Requires write_products 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.


List of new media to be added to the product.

Anchor to productId
productId
required

The ID of the product on which to create the variants.

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.

An array of product variants to be created.


Was this section helpful?

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) {
    userErrors {
      field
      message
    }
    product {
      id
      options {
        id
        name
        values
        position
        optionValues {
          id
          name
          hasVariants
        }
      }
    }
    productVariants {
      id
      title
      selectedOptions {
        name
        value
      }
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-07/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) { userErrors { field message } product { id options { id name values position optionValues { id name hasVariants } } } productVariants { id title selectedOptions { name value } } } }",
 "variables": {
    "productId": "gid://shopify/Product/1072481060",
    "variants": [
      {
        "optionValues": [
          {
            "name": "Red",
            "optionName": "Color"
          },
          {
            "name": "Brand new style",
            "optionId": "gid://shopify/ProductOption/1064576522"
          },
          {
            "name": "Silk",
            "optionName": "Material"
          }
        ],
        "price": 22.0
      },
      {
        "optionValues": [
          {
            "name": "Brand new style",
            "optionName": "Style"
          },
          {
            "id": "gid://shopify/ProductOptionValue/1054672262",
            "optionName": "Color"
          },
          {
            "name": "Silk",
            "optionName": "Material"
          }
        ],
        "price": 23.5
      },
      {
        "optionValues": [
          {
            "name": "Brand new style",
            "optionId": "gid://shopify/ProductOption/1064576522"
          },
          {
            "id": "gid://shopify/ProductOptionValue/1054672263",
            "optionId": "gid://shopify/ProductOption/1064576521"
          },
          {
            "id": "gid://shopify/ProductOptionValue/1054672265",
            "optionId": "gid://shopify/ProductOption/1064576523"
          }
        ],
        "price": 29.5
      }
    ]
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
    productVariantsBulkCreate(productId: $productId, variants: $variants) {
      userErrors {
        field
        message
      }
      product {
        id
        options {
          id
          name
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
      }
      productVariants {
        id
        title
        selectedOptions {
          name
          value
        }
      }
    }
  }`,
  {
    variables: {
      "productId": "gid://shopify/Product/1072481060",
      "variants": [
        {
          "optionValues": [
            {
              "name": "Red",
              "optionName": "Color"
            },
            {
              "name": "Brand new style",
              "optionId": "gid://shopify/ProductOption/1064576522"
            },
            {
              "name": "Silk",
              "optionName": "Material"
            }
          ],
          "price": 22.0
        },
        {
          "optionValues": [
            {
              "name": "Brand new style",
              "optionName": "Style"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672262",
              "optionName": "Color"
            },
            {
              "name": "Silk",
              "optionName": "Material"
            }
          ],
          "price": 23.5
        },
        {
          "optionValues": [
            {
              "name": "Brand new style",
              "optionId": "gid://shopify/ProductOption/1064576522"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672263",
              "optionId": "gid://shopify/ProductOption/1064576521"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672265",
              "optionId": "gid://shopify/ProductOption/1064576523"
            }
          ],
          "price": 29.5
        }
      ]
    },
  },
);

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) {
      userErrors {
        field
        message
      }
      product {
        id
        options {
          id
          name
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
      }
      productVariants {
        id
        title
        selectedOptions {
          name
          value
        }
      }
    }
  }
QUERY

variables = {
  "productId": "gid://shopify/Product/1072481060",
  "variants": [{"optionValues"=>[{"name"=>"Red", "optionName"=>"Color"}, {"name"=>"Brand new style", "optionId"=>"gid://shopify/ProductOption/1064576522"}, {"name"=>"Silk", "optionName"=>"Material"}], "price"=>22.0}, {"optionValues"=>[{"name"=>"Brand new style", "optionName"=>"Style"}, {"id"=>"gid://shopify/ProductOptionValue/1054672262", "optionName"=>"Color"}, {"name"=>"Silk", "optionName"=>"Material"}], "price"=>23.5}, {"optionValues"=>[{"name"=>"Brand new style", "optionId"=>"gid://shopify/ProductOption/1064576522"}, {"id"=>"gid://shopify/ProductOptionValue/1054672263", "optionId"=>"gid://shopify/ProductOption/1064576521"}, {"id"=>"gid://shopify/ProductOptionValue/1054672265", "optionId"=>"gid://shopify/ProductOption/1064576523"}], "price"=>29.5}]
}

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) {
        userErrors {
          field
          message
        }
        product {
          id
          options {
            id
            name
            values
            position
            optionValues {
              id
              name
              hasVariants
            }
          }
        }
        productVariants {
          id
          title
          selectedOptions {
            name
            value
          }
        }
      }
    }`,
    "variables": {
      "productId": "gid://shopify/Product/1072481060",
      "variants": [
        {
          "optionValues": [
            {
              "name": "Red",
              "optionName": "Color"
            },
            {
              "name": "Brand new style",
              "optionId": "gid://shopify/ProductOption/1064576522"
            },
            {
              "name": "Silk",
              "optionName": "Material"
            }
          ],
          "price": 22.0
        },
        {
          "optionValues": [
            {
              "name": "Brand new style",
              "optionName": "Style"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672262",
              "optionName": "Color"
            },
            {
              "name": "Silk",
              "optionName": "Material"
            }
          ],
          "price": 23.5
        },
        {
          "optionValues": [
            {
              "name": "Brand new style",
              "optionId": "gid://shopify/ProductOption/1064576522"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672263",
              "optionId": "gid://shopify/ProductOption/1064576521"
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672265",
              "optionId": "gid://shopify/ProductOption/1064576523"
            }
          ],
          "price": 29.5
        }
      ]
    },
  },
});
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) {
      userErrors {
        field
        message
      }
      product {
        id
        options {
          id
          name
          values
          position
          optionValues {
            id
            name
            hasVariants
          }
        }
      }
      productVariants {
        id
        title
        selectedOptions {
          name
          value
        }
      }
    }
  }
QUERY;

$variables = [
  "productId" => "gid://shopify/Product/1072481060",
  "variants" => [{"optionValues"=>[{"name"=>"Red", "optionName"=>"Color"}, {"name"=>"Brand new style", "optionId"=>"gid://shopify/ProductOption/1064576522"}, {"name"=>"Silk", "optionName"=>"Material"}], "price"=>22.0}, {"optionValues"=>[{"name"=>"Brand new style", "optionName"=>"Style"}, {"id"=>"gid://shopify/ProductOptionValue/1054672262", "optionName"=>"Color"}, {"name"=>"Silk", "optionName"=>"Material"}], "price"=>23.5}, {"optionValues"=>[{"name"=>"Brand new style", "optionId"=>"gid://shopify/ProductOption/1064576522"}, {"id"=>"gid://shopify/ProductOptionValue/1054672263", "optionId"=>"gid://shopify/ProductOption/1064576521"}, {"id"=>"gid://shopify/ProductOptionValue/1054672265", "optionId"=>"gid://shopify/ProductOption/1064576523"}], "price"=>29.5}],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "productId": "gid://shopify/Product/1072481060",
  "variants": [
    {
      "optionValues": [
        {
          "name": "Red",
          "optionName": "Color"
        },
        {
          "name": "Brand new style",
          "optionId": "gid://shopify/ProductOption/1064576522"
        },
        {
          "name": "Silk",
          "optionName": "Material"
        }
      ],
      "price": 22
    },
    {
      "optionValues": [
        {
          "name": "Brand new style",
          "optionName": "Style"
        },
        {
          "id": "gid://shopify/ProductOptionValue/1054672262",
          "optionName": "Color"
        },
        {
          "name": "Silk",
          "optionName": "Material"
        }
      ],
      "price": 23.5
    },
    {
      "optionValues": [
        {
          "name": "Brand new style",
          "optionId": "gid://shopify/ProductOption/1064576522"
        },
        {
          "id": "gid://shopify/ProductOptionValue/1054672263",
          "optionId": "gid://shopify/ProductOption/1064576521"
        },
        {
          "id": "gid://shopify/ProductOptionValue/1054672265",
          "optionId": "gid://shopify/ProductOption/1064576523"
        }
      ],
      "price": 29.5
    }
  ]
}
Hide code
Response
JSON
{
  "productVariantsBulkCreate": {
    "userErrors": [],
    "product": {
      "id": "gid://shopify/Product/1072481060",
      "options": [
        {
          "id": "gid://shopify/ProductOption/1064576521",
          "name": "Color",
          "values": [
            "Blue",
            "Red"
          ],
          "position": 1,
          "optionValues": [
            {
              "id": "gid://shopify/ProductOptionValue/1054672262",
              "name": "Blue",
              "hasVariants": true
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672263",
              "name": "Red",
              "hasVariants": true
            }
          ]
        },
        {
          "id": "gid://shopify/ProductOption/1064576522",
          "name": "Style",
          "values": [
            "Old style",
            "Brand new style"
          ],
          "position": 2,
          "optionValues": [
            {
              "id": "gid://shopify/ProductOptionValue/1054672264",
              "name": "Old style",
              "hasVariants": true
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672267",
              "name": "Brand new style",
              "hasVariants": true
            }
          ]
        },
        {
          "id": "gid://shopify/ProductOption/1064576523",
          "name": "Material",
          "values": [
            "Polyester",
            "Silk"
          ],
          "position": 3,
          "optionValues": [
            {
              "id": "gid://shopify/ProductOptionValue/1054672265",
              "name": "Polyester",
              "hasVariants": true
            },
            {
              "id": "gid://shopify/ProductOptionValue/1054672266",
              "name": "Silk",
              "hasVariants": true
            }
          ]
        }
      ]
    },
    "productVariants": [
      {
        "id": "gid://shopify/ProductVariant/1070325093",
        "title": "Red / Brand new style / Silk",
        "selectedOptions": [
          {
            "name": "Color",
            "value": "Red"
          },
          {
            "name": "Style",
            "value": "Brand new style"
          },
          {
            "name": "Material",
            "value": "Silk"
          }
        ]
      },
      {
        "id": "gid://shopify/ProductVariant/1070325094",
        "title": "Blue / Brand new style / Silk",
        "selectedOptions": [
          {
            "name": "Color",
            "value": "Blue"
          },
          {
            "name": "Style",
            "value": "Brand new style"
          },
          {
            "name": "Material",
            "value": "Silk"
          }
        ]
      },
      {
        "id": "gid://shopify/ProductVariant/1070325095",
        "title": "Red / Brand new style / Polyester",
        "selectedOptions": [
          {
            "name": "Color",
            "value": "Red"
          },
          {
            "name": "Style",
            "value": "Brand new style"
          },
          {
            "name": "Material",
            "value": "Polyester"
          }
        ]
      }
    ]
  }
}