ProductVariantCreate - creating only 1 variant, also creates a default one - GraphQL

Using the GraphQl API documentation (2024-04) to create a product variant, it is creating a new variant, but it is also creating a new default one; “Default Title” with empty values, so now I have 2 variants instead of 1 in the current product.

Is there a way to create a variant without the implicit default one that gets created with it?

Hi,there

could you share your graphql?

Hello, sure

mutation productVariantCreate($input: ProductVariantInput!) {
productVariantCreate(input: $input) {
product {
id
title
}
productVariant {
createdAt
displayName
id
inventoryItem {
unitCost {
amount
}
tracked
}
inventoryPolicy
inventoryQuantity
sku
price
product {
id
}
title
}
userErrors {
field
message
}
}
}


{
“input”: {
“inventoryItem”: {
“cost”: 50,
“tracked”: true
},
“inventoryPolicy”: “DENY”,
“inventoryQuantities”: {
“availableQuantity”: 25,
“locationId”: “gid://shopify/Location/123456789”
},
“price”: 114.99,
“productId”: “gid://shopify/Product/123456789”,
“requiresShipping”: true,
“options”: “Holographic”
}
}

HI,there

you could run the productVariantsBulkCreate mutation with a strategy of REMOVE_STANDALONE_VARIANT and only create one vairant.

mutation productVariantsBulkCreate {
  productVariantsBulkCreate(
    productId: "gid://shopify/Product/1234",
    strategy: REMOVE_STANDALONE_VARIANT,
    variants: [
      # Variants to be created
    ]
  )
}

Hello Eric, thank you for your response.

I tried using productVariantsBulkCreate, I am encountering an issue with this mutation,

how do I handle the optionValues in this case, when I don’t have any variant for the product and want to create a new one.

I am receiving “message”: “You need to add option values for Title” --Or-- option does not exist…

am I missing something?

I will provide my graphql below:

===============================

mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
}
userErrors {
field
message}}}

{“productId”: “gid://shopify/Product/987654321”,
“strategy”: “REMOVE_STANDALONE_VARIANT”,
“variants”: [
{
“optionValues”: [{
“name”: “Red”,
“optionName”: “Color”}],
“inventoryItem”: {
“cost”: 50,
“tracked”: true,
“requiresShipping”: true},
“inventoryPolicy”: “DENY”,
“inventoryQuantities”: {
“availableQuantity”: 25,
“locationId”: “gid://shopify/Location/123456789” },
“price”: 114.99 } ]}

To add: I noticed I have to have at least one option name with one option value to be able to do this request by using existing option name and inserting a new value in the graphql, but shouldn’t I be able to create a new one during productVariantsBulkCreate? what if I have no variant at all in the first place.

Will be waiting for your response,

Thanks!

{“productId”: “gid://shopify/Product/11111”,
“strategy”: “REMOVE_STANDALONE_VARIANT”,
“variants”: [
{
“optionValues”: [{
“name”: “a7”,
“optionName”: “Color”}],
“inventoryItem”: {
“cost”: 50,
“tracked”: true,
“requiresShipping”: true},
“inventoryPolicy”: “DENY”,
“inventoryQuantities”: {
“availableQuantity”: 25,
“locationId”: “gid://shopify/Location/1111” },
“price”: 114.99 } ]}

Hi @nar_77 ,

Are you still blocked?

I’m not 100% sure i understand you usecase, but there are a few options for how you could proceeded. I think the easiest way might be to supply options when you create product and then use variant update to update variant to desired state.

Hello AsafGitai.

Using your answer I was able to reach to a solution with a small addition to it.

Initially I also found another way to deal with it, so both of them work.

1st one: Product Create → Variant Create → query all variants → get the id of the ‘default title’ variant → delete the variant

2nd one: Product Create with option → Product variant update → inventory Quantities update

Thank you all for your answers.

@AsafGitai I have a question: so there is no way to create options within the productVariantsBulkCreate mutation?

2 Likes

I am also wondering about this:

I have a question: so there is no way to create options within the productVariantsBulkCreate mutation?

1 Like