This is the flow that I follow in order to create a product in my Shopify account:
-
I use GraphQL API;
-
The first thing that I do is to use the “ProductSet” Mutation in order to create the Product and send all the images referred to the product;
-
Then I use the “productVariantsBulkUpdate” to pass the images for the variations, but in order to do it I re-use the imageID created with the “ProductSet” Mutation;
-
When I do this it can happen that I receive the following error for some variations:
{
"code": "MUST_BE_FOR_THIS_PRODUCT",
"field": [
"variants",
"5",
"mediaId"
],
"message": "Image must be for this product"
}
Also I’ve noticed that this error happens when I try to import the same product with same options and images more than one time in a short time.
If I repeat again the mutation the error doesn’t appear.
Following the two mutations as examples:
ProductSet Mutation:
mutation CreateProductSynchronous($productSet: ProductSetInput!, $synchronous: Boolean!){
productSet(input: $productSet, synchronous: $synchronous) {
product {
id
title
options {
id
name
optionValues {
id
name
}
}
media(first:250){
edges{
node{
id
alt
preview{
image{
id
url
}
}
}
}
}
variants(first: 100) {
edges {
node {
id
title
price
sku
}
}
}
}
userErrors {
field
message
}
}
}
variables...
productVariantsBulkUpdate Mutation:
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
media(first:1) {
edges {
node {
id
}
}
}
}
userErrors {
code
field
message
}
}
}
"""variables"": {
""productId"": ""gid://shopify/Product/8837826937090"",
""variants"": [
{
""id"": ""gid://shopify/ProductVariant/45598512513282"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512513282""
},
""mediaId"": ""gid://shopify/MediaImage/34440417673474"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512546050"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512546050""
},
""mediaId"": ""gid://shopify/MediaImage/34440417771778"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512578818"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512578818""
},
""mediaId"": ""gid://shopify/MediaImage/34440417870082"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512611586"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512611586""
},
""mediaId"": ""gid://shopify/MediaImage/34440417706242"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512644354"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512644354""
},
""mediaId"": ""gid://shopify/MediaImage/34440417804546"",
""price"": 17.5,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512677122"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512677122""
},
""mediaId"": ""gid://shopify/MediaImage/34440417902850"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512709890"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512709890""
},
""mediaId"": ""gid://shopify/MediaImage/34440417739010"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512742658"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512742658""
},
""mediaId"": ""gid://shopify/MediaImage/34440417837314"",
""price"": 17.0,
""taxable"": true
},
{
""id"": ""gid://shopify/ProductVariant/45598512775426"",
""inventoryPolicy"": ""DENY"",
""inventoryItem"": {
""requiresShipping"": true,
""tracked"": true,
""sku"": ""ZAK-45598512775426""
},
""mediaId"": ""gid://shopify/MediaImage/34440417935618"",
""price"": 17.0,
""taxable"": true
}
]
}"
Response:
"""productVariantsBulkUpdate"": {
""product"": {
""id"": ""gid://shopify/Product/8837826937090""
},
""productVariants"": [
{
""id"": ""gid://shopify/ProductVariant/45598512513282"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512546050"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512578818"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512611586"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512644354"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512677122"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512709890"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512742658"",
""media"": {
""edges"": []
}
},
{
""id"": ""gid://shopify/ProductVariant/45598512775426"",
""media"": {
""edges"": []
}
}
],
<strong>""userErrors"": [
{
""code"": ""MUST_BE_FOR_THIS_PRODUCT"",
""field"": [
""variants"",
""0"",
""mediaId""
],
""message"": ""Image must be for this product""
},
{
""code"": ""MUST_BE_FOR_THIS_PRODUCT"",
""field"": [
""variants"",
""5"",
""mediaId""
],
""message"": ""Image must be for this product""
}
]</strong>
}"
As you can see I can receive the error: MUST_BE_FOR_THIS_PRODUCT
How can I resolve this error?