Retrieve Product Variant
Check whether a variant is in stock.
On this page
In this guide, you'll learn how to retrieve a product variant's inventory quantity in a storefront.
How to Retrieve a Product Variant's Inventory Quantity?
To retrieve variants' inventory quantity using either the List Products or Retrieve Products API routes:
- Pass in the
fieldsquery parameter the value+variants.inventory_quantity.- When also retrieving prices, make sure to include
*variants.calculated_pricein the beginning of the list of fields. For example,?fields=*variants.calculated_price,+variants.inventory_quantity.
- When also retrieving prices, make sure to include
- Pass the publishable API key in the header of the request, which you always do when sending a request to the Store API. The inventory quantity is retrieved based on the stock locations of the sales channels that belong to the API key's scope.
- If you're using the JS SDK, the publishable API key is automatically passed in the header of the requests as explained in the Publishable API Keys guide.
For example:
Important: If you're also passing *variants.calculated_price in fields to get the product variants' prices, make sure to include it in the beginning of the list of fields. For example, ?fields=*variants.calculated_price,+variants.inventory_quantity.
ts
In this example, you retrieve the product variants' inventory quantity by passing +variants.inventory_quantity in the fields query parameter. This will add a new inventory_quantity field to each variant object.
When is a Variant in Stock?
A variant is in stock if:
- Its
manage_inventory's value isfalse, meaning that Aiminaabee doesn't keep track of its inventory. - If its
inventory_quantity's value is greater than0.- This property is only available on variants whose
manage_inventoryisfalse. - If the variant doesn't have inventory levels in the stock location associated with the API's scope, its
inventory_quantitywill benull.
- This property is only available on variants whose
Full React Example
For example, to show on a product's page whether a variant is in stock in a React-based storefront:
Tip: Learn how to install and configure the JS SDK in the Getting started guide.
tsx
In this example, you retrieve the product variants' inventory quantity by passing +variants.inventory_quantity in the fields query parameter. This will add a new inventory_quantity field to each variant object.
Then, you find the selected variant and show whether it's in stock. The variant is in stock if its manage_inventory property is disabled, or the inventory_quantity is greater than 0.
Tip: Refer to the Select Product Variants guide to learn more about selecting a product variant.