Product creation with a single variant that tracks inventory using 2024-04 API

I’m migrating some existing GraphQL code that is no longer compatible with the new 2024-04 API.

My requirements are:

  • Create a product with a single/default variant, and leave it with status Draft
  • The variant must have a SKU
  • The variant quantity must be tracked in inventory.
  • The variant must have qty 1 in a location that is not enabled by default when the product is created
  • Publish the product to two Sales Channels: Online Store and Point of Sale

Before, I was able to achieve what I needed in 2 GraphQL API Calls:

  1. productCreate: I created the product with the inventoryItem > tracked: True flag, SKU, and inventory Quantities for a non default Inventory Location.

  2. publishablePublish: Published the product to the Online Store and Point of Sale sales channels. Later when the product status is changed to Active, these Sales Channels are already enabled.

With the 2024-04 API Changes, it seems that I need to make 5 API Calls:

  1. productCreate: I can no longer specify any variant information, so the product cannot be marked as tracked by inventory or with a SKU.

  2. inventoryActivate: For the default product variant InventoryItem, I activate an Inventory Location that is not enabled by default.

  3. productVariantUpdate: For the default product variant, I add the SKU, taxable: True, inventoryItem > tracked: true. Here, if I add inventoryQuantities as part of the mutation input, it doesn’t modify the available quantity or come back with an error. The rest of the data is updated Ok. Is this a bug?

  4. inventorySetOnHandQuantities: For the default product variant InventoryItem, I set the quantity to 1.

  5. publishablePublish: I publish the product to the Online Store and Point of Sale sales channels.

Am I approaching this the correct way? Or would there be a better way to achieve my requirements with the new API?

5 API calls seems an exaggerate number to add one product with the desired properties. Also, there’s a higher probability of API transient errors to break the expected functionality (e.g.: Adding the product, but not assigning inventory quantities).

Thanks!

2 Likes

I ran into this today as well. Here is the solution:

  1. Do a productCreate

  2. Do a productVariantBulkCreate with one single variant and strategy REMOVE_STANDALONE_VARIANT

Works like a charm

3 Likes

Thanks!.. This worked :smiling_face_with_sunglasses:

keep getting post Response Logging: {“errors”:[{“message”:“Variable $productId of type ID! was provided invalid value”,“locations”:[{“line”:2,“column”:68}],“extensions”:{“value”:null,“problems”:[{“path”:,“explanation”:“Expected value to not be null”}]}},{“message”:“Variable $variants of type [ProductVariantsBulkInput!]! was provided invalid value”,“locations”:[{“line”:2,“column”:85}],“extensions”:{“value”:null,“problems”:[{“path”:,“explanation”:“Expected value to not be null”}]}}]} bookdelivered.myshopify.com error. Please help

What’ the query look like?

Does your productId start with gid://shopify/Product ?

These are how my mutations look like:

mutation productCreate($input: ProductInput!) {
productCreate(input: $input) {
product {
id
}
userErrors {
field
message
}
}
}

{
“input”: {
“title”: “Product Title”,
“productType”: “Product X”,
“vendor”: “Vendor X”,
“descriptionHtml”: “Product description”,
“status”: “DRAFT”,
“metafields”: [
{
“namespace”: “custom_fields”,
“key”: “my_custom_field”,
“type”: “single_line_text_field”,
“value”: “Some value..”
}
]
}
}


Then using the productId generated by the previous mutation:

mutation productVariantsBulkCreate($productId: ID!, $strategy: ProductVariantsBulkCreateStrategy, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkCreate(productId: $productId, strategy: $strategy, variants: $variants) {
product {
id
}
userErrors {
field
message
}
}
}

{
“productId”: “gid://shopify/Product/123456789”,
“strategy”: “REMOVE_STANDALONE_VARIANT”,
“variants”: [
{
“inventoryItem”: {
“requiresShipping”: true,
“tracked”: true
},
“inventoryPolicy”: “DENY”,
“inventoryQuantities”: [
{
“availableQuantity”: 1,
“locationId”: “gid://shopify/Location/123456789”
},
{
“availableQuantity”: 0,
“locationId”: “gid://shopify/Location/987654321”
}
],
“sku”: “SKU_ABC”,
“taxable”: true
}
]
}

Hope this helps..

3 Likes

Thank you so much for this, the Shopfiy API is like the Masonic handshake out of Monty Python and I have worked on quite a few APIs in my time…