Hi guys, I’ve had an issue where creating new variants for a product using the productVariantsBulkCreate deletes the standalone variant from the products and replaces it with the new variant.
The input requests a mandatory option for the new variant being created and even when using Title which is the default option the standalone variant gets deleted.
This has been very problematic for us since we have an app that creates variants on the fly and most of the products from our customers are using Options for Weight but there are some merchants that didn’t have a setup for a Weight Option and our app has been basically wiping out the standalone variants for multiple products.
This is an example of a query used to get a product details and the reply using graphqli from the admin dashboard:
Query
query product($id: ID!) {
product(id: $id) {
options {
name
optionValues {
name
}
}
variants(first: 100) {
edges {
node {
title
inventoryItem {
measurement {
weight {
value
unit
}
}
}
}
}
}
}
}
Variables
{
"id": "gid://shopify/Product/8198741557409"
}
Data returned
{
"data": {
"product": {
"options": [
{
"name": "Title",
"optionValues": [
{
"name": "Default Title"
}
]
}
],
"variants": {
"edges": [
{
"node": {
"sku": "10010101",
"title": "Default Title",
"inventoryItem": {
"measurement": {
"weight": {
"value": 100,
"unit": "GRAMS"
}
}
}
}
}
]
}
}
}
}
Mutation
mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, variants: $variants, strategy: DEFAULT) {
productVariants {
id
title
price
barcode
taxable
sku
product {
id
featuredMedia {
preview {
image {
url
}
}
}
}
inventoryItem {
measurement {
weight {
unit
value
}
}
}
selectedOptions {
name
optionValue {
id
name
}
}
}
userErrors {
field
message
}
}
}
Variables
{
"productId": "gid://shopify/Product/8198741557409",
"variants": [
{
"price": "0.40",
"inventoryItem": {
"tracked": false,
"sku": "10010101",
"measurement": {
"weight": {
"value": 200,
"unit": "GRAMS"
}
}
},
"inventoryPolicy": "CONTINUE",
"optionValues": [
{
"optionName": "Title",
"name": "200g"
}
],
"taxable": false
}
]
}
Response
{
"data": {
"productVariantsBulkCreate": {
"productVariants": [
{
"id": "gid://shopify/ProductVariant/44844161958049",
"title": "200g",
"price": "0.40",
"barcode": null,
"taxable": false,
"sku": "10010101",
"product": {
"id": "gid://shopify/Product/8198741557409",
"featuredMedia": {
"preview": {
"image": {
"url": "https://cdn.shopify.com/s/files/1/0601/1879/4401/products/Jumbo-Oats_d8527fb9-6068-4369-a517-ecbfbb1ffb27.jpg?v=1639517605"
}
}
}
},
"inventoryItem": {
"measurement": {
"weight": {
"unit": "GRAMS",
"value": 200
}
}
},
"selectedOptions": [
{
"name": "Title",
"optionValue": {
"id": "gid://shopify/ProductOptionValue/3934731174049",
"name": "200g"
}
}
]
}
],
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 17,
"actualQueryCost": 17,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1983,
"restoreRate": 100
}
}
}
}
As you can see the initial standalone variant has been wiped out and its no longer there.
Re executing the product query shows that thers only one variant
{
"data": {
"product": {
"options": [
{
"name": "Title",
"optionValues": [
{
"name": "200g"
},
{
"name": "Default Title"
}
]
}
],
"variants": {
"edges": [
{
"node": {
"title": "200g",
"sku": "10010101",
"price": "0.40",
"inventoryItem": {
"measurement": {
"weight": {
"value": 200,
"unit": "GRAMS"
}
}
}
}
}
]
}
}
},
"extensions": {
"cost": {
"requestedQueryCost": 23,
"actualQueryCost": 7,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1993,
"restoreRate": 100
}
}
}
}
And the option is forced in the input of the create bulk variants, I tried to specify Weight but it doesn’t accept it. Looks to me that at least if the Title is specified the default strategy for ProductVariantsBulkCreateStrategy should be respected and the standalone variant not deleted.