GraphQL API: How to check if variant is available in the Online store sales channel?

From the ProductVariant fields, I noticed that I can use availableForSale, but it looks like this would also return true it the variant is available in other sales channel that’s not the online store.

Also, I saw the sellableOnlineQuantity looks promising, but if the inventoryPolicy field is set to CONTINUE (selling when out of stock) then the sellableOnlineQuantity field doesn’t say much.

Any insight is appreciated, maybe I’m just confused about these fields!

Hey @Pridentt, is your main goal to see if a variant has inventory available, or if it is available to the online store?

It’s not possible to filter or set publications at the variant level, so you can use the published_status:published on a products query to return all of the products published to the online store.

For the availableForSale field, this is false only when the variant tracks inventory, denies overselling, and has 0 (or less) total available quantity across all locations. In all other cases it returns true.

So to find products available to the online store and able to be sold, combining published_status:published WITH availableForSale: true would work.

Hi @KyleG-Shopify, sorry maybe I should have specified, I’m basically trying to validate if the variant is available to be added to cart in the storefront.

I ended up using this query:

query VariantOnlineInventoryMany($ids: [ID!]!) {
    nodes(ids: $ids) {
      ... on ProductVariant {
        id
        availableForSale
        product {
          status
        }
      }
    }
  }

Then validating that if availableForSale === true AND product.status is ACTIVE or UNLISTED, then I consider it available to be added in the storefront.

Is this correct? Or should I also consider published_status? Thanks!

That would work, but it could still lead to some issues as it’s a little bit too generalized. If the shop is using markets or other contextualized features, the buyers may end up in a checkout that isn’t valid for them.

If you’re building carts, I would recommend the storefront API instead to ensure the buyer context is used

Yeah I don’t have a problem with that, I’m not building carts, I was just getting this data to show alerts in my app’s UI, thanks Kyle!