Can I get the available quantity with the cart API?

Short description of issue

Cannot show available quantity on hand when there is not enough quantity

Reproduction steps

My app adds multiple items to the cart and group them to a bundle. When a customer orders 8 items but there is only 7, the cart API can only show “The item.name is sold out”. This is the standard error response.

Can I get the available inventory on hand? I want the API to give me the item name with the available quantity like 7. Then I can tell users to remove 1 and proceed.

I don’t see any way possible. The Storefront API is too heavy for this issue.

Additional info

What type of topic is this

Feature request

1 Like

Hey @Benny_Chan :waving_hand: thanks for reaching out - I noticed you mentioned that the Storefront API wouldn’t work, but the easiest way to pull this info would be through the SFAPI by checking a product variant’s available quantities (**quantityAvailable):
**

One thing to note though, this would pull all available inventory for a specific variant from all sources that make it available for sale online, just wanted to mention that. I’m more than happy to put through a feature request for you though. If you’re open to sharing a bit more about your use case and why the SFAPI method wouldn’t work, I’m happy to include that in the request I’ll log on my end - hope to hear from you soon!

Thanks for your prompt reply, @Alan_G . Yes, I can talk more about my use case:

A merchant wants to sell a custom bundle of 18 cupcakes for a fixed price like $100. The flavors can be mixed from the cupcake collection.

To offer a fixed price discount, I can only use an expand operation, which adds the parent product to the cart.

Let’s say a customer adds a 9 chocolate-flavor cupcake, but there are only 7. The cart API can only give a response of {{ parent_product.name }} is sold out.

But the user wants to tell the customer to remove (9-7) = 2 chocolate cupcakes from the bundle and continue. The first message will tell customers to leave because the bundle sold out. The user thinks that it doesn’t make sense.

If you want to have the merchant’s store info, you can pm me.

I only need to check the inventory quantities when a customer adds a bundle to the cart. The solutions I think of:

  • liquid - no inventory quantity :cross_mark:
  • product.json endpoint - also hidden the quantity :cross_mark:
  • storefront API
    • quite heavyweight, considering I only use it when adding a bundle to the cart in a theme app extension
    • also takes a long time? I only need the sold-out item name and available quantity. If the customer adds 15 distinct variants, I have to check them at once.

Thanks for the confirmation here @Benny_Chan, that use case definitely makes a lot of sense. I’ll reach out internally to see if we have any workarounds I’m missing or if this is on our roadmap, but at the very least I’m more than happy to log a feature request for you. Speak with you soon!

Hey @Benny_Chan :waving_hand: - I was able to speak with one of our product component developers today and our reccomendation is to use the Storefront API. That said, I definitely understand that querying quite a few variants/products in one go could affect performance or return too much data, so our workaround would be to use our query filters as needed:

Let me know if this is still missing some requirements for your use case and I’d be happy to log that feature request for you. Hope this helps a little bit.

Hey @Alan_G

Thanks for your suggestion. Your query only selects a list of products but I have to select multiple product variants, e.g <= 20 variants from different products. Can the storefront API support this query? I don’t find it now.

If I have to query the variants one by one, these queries will take a few seconds for more than 10 variants. This process is too long for adding a bundle to cart.

Hey @Benny_Chan , no problem, appreciate you following up. I did a bit more testing, and found a method that may work as a workaround.

You should be able to fetch more than 20 variants in a single request by using the nodes(ids: [ID!]!) pattern in the Storefront API and passing an array of variant GIDs as your variables.

In my testing, this scales pretty well for dozens of variants, and you can always batch into multiple calls if you’re working with very large sets to stay within GraphQL cost limits. I’ve got a quick example below that shows the exact query and a fairly large variables payload:

query VariantAvailability($ids: [ID!]!) {
  nodes(ids: $ids) {
    ... on ProductVariant {
      id
      title
      quantityAvailable
      availableForSale
    }
  }
}

Variables:

{ "ids": [  "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here",
"gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here", "gid://shopify/ProductVariant/variant-here" ] }

Hope this helps as always, let me know if I can clarify anything on my end here :slight_smile:

Thanks, @Alan_G . Your code works well. You are the best!

Hey @Benny_Chan, no worries, glad this helped!