Problem whit mutuation productVariantsBulkCreate

Hi everyone, I'll get straight to the point.
I'm trying to import a product from my Yii2 site to my Shopify store.
This is my idea:
I create the product with the productCreate mutation.
I create the variants with productVariantsBulkCreate.
But it gives me an error saying that a variant already exists and is created by default. How can I bypass this? Since they're dynamic, I have no way of knowing which variant has already been created.

The product create mutation can return you the product and the default variant that it created, if you query them in the returned product value.

Alternatively you can use the product set mutation to do tye product and variants in one go.

1 Like

Hey all :waving_hand: - just +1ing @JordanFinners here and adding a bit of context. It is expected behaviour that when a product is created, a “default variant” is always created alongside it. This happens since the fulfillment/inventory management features on Shopify depend on product variants/merchandise to exist in order to process things correctly.

The productSet mutation Jordan shared above is the best method to set up products and upon product creation you should be able to include the child variant as a field you want surfaced in the API response. Hope this helps as always! :slight_smile:

Thanks guys!! I looked at the mutation product set and tested it with the examples: so my array is:

$productSetInput = [
      'synchronous' => true,
      'productSet' => [
        'title' => 'Winter hat',
        'productOptions' => [
          [
            'name' => 'Color',
            'position' => 1,
            'values' => [
              [
                'name' => 'Grey'
              ],
              [
                'name' => 'Black'
              ]
            ]
          ]
        ],
        'variants' => [
          [
            'optionValues' => [
              [
                'optionName' => 'Color',
                'name' => 'Grey'
              ]
            ],
            'inventoryQuantities' => [
              [
                'locationId' => $locationId,
                'name' => 'available',
                'quantity' => 12
              ],
            ],
            'price' => 79.99
          ],
          [
            'optionValues' => [
              [
                'optionName' => 'Color',
                'name' => 'Black'
              ]
            ],
            'inventoryQuantities' => [
              [
                'locationId' => $locationId,
                'name' => 'available',
                'quantity' => 976
              ],
            ],
            'price' => 11.99
          ]
        ]
      ]
    ];

and mutation is:

$mutation = <<<GQL
mutation createProduct(\$productSet: ProductSetInput!, \$synchronous: Boolean!) {
  productSet(synchronous: \$synchronous, input: \$productSet) {
    product {
      id
      variants(first: 5) {
        nodes {
          title
          price
          inventoryQuantity
          inventoryItem {
            inventoryLevels(first: 5) {
              nodes {
                location {
                  id
                  name
                }
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}
GQL;

The product is created correctly, but the quantities remain undefined. Is there a reason? Where could I have gone wrong? Thanks.

If you are creating a new product with the inventory, you’ll need to set the inventory item to be tracked in your input productSet - GraphQL Admin

1 Like