How to get a variant's inventory quantity?

How to get a variant's inventory quantity?

Dzonotonas
Shopify Partner
33 2 15

Hello, 👋

 

GET `/products/handle.json` return an JSON:

 

{
  product: {
    id: 1,
    title: "title",
    body_html: 'body',
    vendor: "vendor",
    ...
    variants: [
      {
        id: 1,
        product_id: 2,
        title: "variant title",
        ...
      },
    ],
  ...
  },
}

JSON does not contain information about the variant's inventory quantity. How can I retrieve this data? I need it on the home page. I'm looking a solution for the Shopify theme app extension (I need this data in the app extension), so modifying the store's theme Liquid files is not an option.

Replies 2 (2)

DaisyVo
Shopify Partner
4469 501 598

Hi @Dzonotonas 

You can refer to this video

https://www.youtube.com/watch?v=WMdYdvrFQ1s

Please let us know if our reply is helpful by giving it a Like or marking it as a Solution!

Avada SEO & Image Optimizer - The #1 SEO solution

hasbi_expatify
Shopify Partner
18 0 1

 

Querying Product Variant Inventory in Shopify

The following GraphQL query retrieves inventory details for a specific product variant:

 

 
query GetVariantInventory {
productVariant(id: "gid://shopify/ProductVariant/43729076") {
id
title
inventoryQuantity
availableForSale
}
}

Explanation:

  • id: Replace this with the actual product variant ID you want to query.
  • Fields:
    • id: The unique identifier of the product variant.
    • title: The name of the variant.
    • inventoryQuantity: The current stock level of the variant.
    • availableForSale: Indicates whether the variant is available for purchase.

Important Note:

Make sure your app has the read_inventory access scope enabled to retrieve inventory data.
and also refer to here https://shopify.dev/docs/api/admin-graphql/2024-10/queries/productVariants

If you like our solution, please like and mark it as a solution.
Don't hesitate to reach us for more Shopify solutions. Thanks 
Website: Expatify Indonesia
Email: hasbi@expatify.co.id