Image must be for a product while using productVariantsBulkCreate mutation

I am facing this issue while using productVariantsBulkCreate mutation api . (2024-04 admin api version )

productVariantsBulkCreate Input

{
  "productGid": "gid://shopify/Product/8550154928285",
  "variationData": [
    {
      "price": 20.95,
      "sku": "733739029430",
      "inventoryItem": {
        "tracked": true
      },
      "inventoryQuantities": {
        "availableQuantity": 6,
        "locationId": "gid://shopify/Location/76652937373"
      },
      "weight": 0,
      "weightUnit": "POUNDS",
      "optionValues": [
        {
          "name": "2400 GDU/g - 500mg - 60 veg capsules",
          "optionName": "Variation"
        }
      ],
      "mediaId": "gid://shopify/MediaImage/30748160327837"
    },
    {
      "price": 38.95,
      "sku": "733739029478",
      "inventoryItem": {
        "tracked": true
      },
      "inventoryQuantities": {
        "availableQuantity": 12,
        "locationId": "gid://shopify/Location/76652937373"
      },
      "weight": 0,
      "weightUnit": "POUNDS",
      "optionValues": [
        {
          "name": "2400 GDU/g - 500mg - 120 veg capsules",
          "optionName": "Variation"
        }
      ],
      "mediaId": "gid://shopify/MediaImage/30748160360605"
    }
  ]
}

Failed productVariantsBulkCreate Response

{
  "productGid": "gid://shopify/Product/8550154928285",
  "response": {
    "productVariants": [],
    "userErrors": [
      {
        "field": [
          "variants",
          "0",
          "mediaId"
        ],
        "message": "Image must be for this product",
        "code": "MUST_BE_FOR_THIS_PRODUCT"
      },
      {
        "field": [
          "variants",
          "1",
          "mediaId"
        ],
        "message": "Image must be for this product",
        "code": "MUST_BE_FOR_THIS_PRODUCT"
      }
    ]
  },
  "variables": {
    "productId": "gid://shopify/Product/8550154928285",
    "strategy": "REMOVE_STANDALONE_VARIANT",
    "variants": [
      {
        "price": 20.95,
        "sku": "733739029430",
        "inventoryItem": {
          "tracked": true
        },
        "inventoryQuantities": {
          "availableQuantity": 6,
          "locationId": "gid://shopify/Location/76652937373"
        },
        "weight": 0,
        "weightUnit": "POUNDS",
        "optionValues": [
          {
            "name": "2400 GDU/g - 500mg - 60 veg capsules",
            "optionName": "Variation"
          }
        ],
        "mediaId": "gid://shopify/MediaImage/30748160327837"
      },
      {
        "price": 38.95,
        "sku": "733739029478",
        "inventoryItem": {
          "tracked": true
        },
        "inventoryQuantities": {
          "availableQuantity": 12,
          "locationId": "gid://shopify/Location/76652937373"
        },
        "weight": 0,
        "weightUnit": "POUNDS",
        "optionValues": [
          {
            "name": "2400 GDU/g - 500mg - 120 veg capsules",
            "optionName": "Variation"
          }
        ],
        "mediaId": "gid://shopify/MediaImage/30748160360605"
      }
    ]
  }
}

Just created product response

 "data": {
      "productCreate": {
        "product": {
          "id": "gid://shopify/Product/8550154928285",
          "media": {
            "nodes": [
              {
                "id": "gid://shopify/MediaImage/30748160295069",
                "alt": "Now Foods Bromelain",
                "mediaContentType": "IMAGE",
                "mediaErrors": [],
                "mediaWarnings": [],
                "status": "UPLOADED",
                "preview": {
                  "status": "Over 9 levels deep, aborting normalization"
                }
              },
              {
                "id": "gid://shopify/MediaImage/30748160327837",
                "alt": "Now Foods Bromelain",
                "mediaContentType": "IMAGE",
                "mediaErrors": [],
                "mediaWarnings": [],
                "status": "UPLOADED",
                "preview": {
                  "status": "Over 9 levels deep, aborting normalization"
                }
              },
              {
                "id": "gid://shopify/MediaImage/30748160360605",
                "alt": "Now Foods Bromelain",
                "mediaContentType": "IMAGE",
                "mediaErrors": [],
                "mediaWarnings": [],
                "status": "UPLOADED",
                "preview": {
                  "status": "Over 9 levels deep, aborting normalization"
                }
              }
            ]
          }
        },
        "userErrors": []
      }
    }

Hi Shramik,

You may need to use the productVariantAppendMedia mutation to associate the media with the variants. It would look something like:

     mutation AppendMediaToVariant {
       productVariantAppendMedia(
         productId: "gid://shopify/Product/8550154928285",
         variantMedia: [
           {
             variantId: "gid://shopify/ProductVariant/VARIANT_ID",
             mediaIds: ["gid://shopify/MediaImage/30748160327837"]
           }
         ]
       ) {
         product {
           id
         }
         productVariants {
           id
           media(first: 10) {
             edges {
               node {
                 id
                 mediaContentType
               }
             }
           }
         }
         userErrors {
           field
           message
         }
       }
     }

Can you try this first and then the productVariantsBulkCreate mutation again?

1 Like

Hi , @Liam-Shopify thanks for the reply .
i guess you meant to say ,
Step 1 : first createProduct with media associated .
Step 2 : then productVariantsBulkCreate without MediaId

productVariantsBulkCreate Input

{
  "productGid": "gid://shopify/Product/8550154928285",
  "variationData": [
    {
      "price": 20.95,
      "sku": "733739029430",
      "inventoryItem": {
        "tracked": true
      },
      "inventoryQuantities": {
        "availableQuantity": 6,
        "locationId": "gid://shopify/Location/76652937373"
      },
      "weight": 0,
      "weightUnit": "POUNDS",
      "optionValues": [
        {
          "name": "2400 GDU/g - 500mg - 60 veg capsules",
          "optionName": "Variation"
        }
      ],
    },
    {
      "price": 38.95,
      "sku": "733739029478",
      "inventoryItem": {
        "tracked": true
      },
      "inventoryQuantities": {
        "availableQuantity": 12,
        "locationId": "gid://shopify/Location/76652937373"
      },
      "weight": 0,
      "weightUnit": "POUNDS",
      "optionValues": [
        {
          "name": "2400 GDU/g - 500mg - 120 veg capsules",
          "optionName": "Variation"
        }
      ],
    }
  ]
}

get the list of variants created .

Step 3 : Map the mediaId from Step 1 with respective variantId from step 2
and input in the productVariantAppendMedia mutation .

Am i right in this flow ? @Liam-Shopify