Storefont API: How to get number of items in stock?

Highlighted
Shopify Partner
2 0 0

hi,

using the storefront API (https://github.com/Shopify/js-buy-sdk), how can I find out if a product / variant is currently in stock? I basically need to know if the product can be purchased at the moment or not. — how would I do that?

———

I am also confused about the `available` field on products. according to the API it "Indicates if at least one product variant is available for sale". it always seems to be `false` though, even for products with variants that are in fact in stock.

thanks a lot in advance!

0 Likes
Highlighted
Shopify Partner
1840 177 721

Weird that. I haven't used the JS Buy SDK, but as it is essetially just a convenience wrapper around a few common GraphQL queries on Storefront API, I'd assume they behave same.

When querying a product that has 3 variants of which 2 are stocked and 1 is out of stock, where out of stock products are set not to be sold:

{
  shop {
    products(first: 1) {
      edges {
        node {
          availableForSale
          variants(first: 3) {
            edges {
              node {
                title
                availableForSale
              }
            }
          }
        }
      }
    }
  }
}

I get the expected result:

{
  "data": {
    "shop": {
      "products": {
        "edges": [
          {
            "node": {
              "availableForSale": true,
              "variants": {
                "edges": [
                  {
                    "node": {
                      "title": "S",
                      "availableForSale": true
                    }
                  },
                  {
                    "node": {
                      "title": "M",
                      "availableForSale": true
                    }
                  },
                  {
                    "node": {
                      "title": "L",
                      "availableForSale": false
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}

Where availableForSale is the equivent of available property in JS Buy SDK.

Suggest you check product variant entries and inventory. Are you using Shopigy to track inventory?

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
0 Likes
Highlighted
New Member
2 0 0

it seems like the Shopify buy SDK does not give you the 'availableForSale' property in product.options. It is only available in the product. In this case, a product is only unavailable when ALL of the variants are sold out. I need to conditionally show each variant being sold out. Not wait for all of them to be sold out for a product to become unavailable.

0 Likes