Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: productVariantUpdate not update the quantity of my variant

productVariantUpdate not update the quantity of my variant

Leobar00
Shopify Partner
12 0 3

I would like to update quantity of single variant with the graphql .
I understand that I can't use just the variant id to do this, how can I do this if I only have the variant id and the product id and the locations ?

I have now tried to use productVariantUpdate but it is not updating the quantity .
whereas with productUpdate I update the product but if I don't specify the variants it deletes them right?

Replies 7 (7)

Liam
Community Manager
3108 344 904

Hi Leobar00,

 

You're correct in saying that the productVariantUpdate mutation doesn't directly update the inventory quantity. Instead, you need to use the inventoryQuantityAdjust or inventoryQuantitySet mutation to adjust or set the inventory quantity of a variant.

Here's an example of how you can use inventoryQuantityAdjust mutation:

mutation {
  inventoryQuantityAdjust(input: {
    inventoryLevelId: "gid://shopify/InventoryLevel/123456789",
    availableDelta: 10
  }) {
    inventoryLevel {
      id
      available
    }
    userErrors {
      field
      message
    }
  }
}

In this mutation, inventoryLevelId is the ID of the inventory level that you want to adjust, and availableDelta is the number by which you want to adjust the available inventory quantity.

And yes, you're correct about the productUpdate mutation as well. If you update a product and only include some variants in the update, then any variants not included will be deleted. To safely manage variants without the risk of deleting excluded variants, you can use productVariantsBulkUpdate.

Here's how you can use productVariantsBulkUpdate:

mutation {
  productVariantsBulkUpdate(variants: [{ id: "gid://shopify/ProductVariant/1234567890", price: "10.00" }]) {
    productVariants {
      id
      price
    }
    userErrors {
      field
      message
    }
  }
}

In this mutation, you will need to pass in the variant id and the updated price. You can replace the price field with any other field you want to update.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Leobar00
Shopify Partner
12 0 3

Thanks @Liam , 
what is the best way to update product quantities by having the id of the product, variant and inventoryItem ?
I am passed the total quantity in the inventory so the delta would require calculations that I would rather not do .
Thank you very much for your support

Liam
Community Manager
3108 344 904

Hi again Leobar00,

 

The best way to update product quantities with the data you have could be to use the /inventory_levels/set.json endpoint. If you ever need to adjust the inventory by a delta (increase or decrease), you can use the adjust endpoint: /admin/api/2023-07/inventory_levels/adjust.json

 

More info on these from our dev docs here

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Leobar00
Shopify Partner
12 0 3

Thanks @Liam ,
I use the graphql API , and i would like to update only available field of quantity .
so that those in the orders are not counted and only those available are updated,:
which mutation for graphql should I use?

Leobar00
Shopify Partner
12 0 3

@LiamI cannot find the inventoryQuantitySet you mentioned in the previous answer could you send me the link to the documentation ? Thanks

Leobar00
Shopify Partner
12 0 3

@Liam with productVariantsBulkUpdate mutation can i update the quantities of a variant ? 

rpbiwer
Shopify Partner
19 0 7

Hi Liam. I'm hoping you can elaborate on this: productVariantUpdate, though it accepts inventoryQuantities input, does not actually update the inventory quantity? May I ask what the reasoning is for this? It's very confusing behavior as the GraphQL input is accepted but seems to silently fail to adjust the quantity. At the very least it would be helpful if the mutation returned an error or warning if inventoryQuantities was passed in.