What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

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

Solved

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

nar_77
Shopify Partner
5 1 0

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?

 

1.png

 

 

Accepted Solutions (3)
Eric-HAN
Shopify Partner
263 29 27

This is an accepted solution.

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
    ]
  )
}
- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee

View solution in original post

AsafGitai
Shopify Staff
109 16 45

This is an accepted solution.

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.

View solution in original post

nar_77
Shopify Partner
5 1 0

This is an accepted solution.

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.

View solution in original post

Replies 9 (9)

Eric-HAN
Shopify Partner
263 29 27

Hi,there 

could you share your graphql?

- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee
nar_77
Shopify Partner
5 1 0

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"
  }
}

 

 

Eric-HAN
Shopify Partner
263 29 27

This is an accepted solution.

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
    ]
  )
}
- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee
nar_77
Shopify Partner
5 1 0

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 } ]}

 

Untitled.png

 

nar_77
Shopify Partner
5 1 0

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 } ]}
AsafGitai
Shopify Staff
109 16 45

This is an accepted solution.

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.

nar_77
Shopify Partner
5 1 0

This is an accepted solution.

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.

stefanb1234
Shopify Partner
16 0 9

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

Brian_Koiter
Shopify Partner
3 0 1

I am also wondering about this:

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