How to Access Product Inventory Count in Shopify Functions (Cart/Checkout/Discount)?

Hi Shopify Team,

We are currently working with Shopify Functions (such as Cart & Checkout Validation, Cart Transform, and Discount Functions).

Our use case requires us to check the product inventory count in order to apply certain rules or restrictions. However, we are unable to find a way to directly access inventory data inside these functions.

Some questions we have:

  • Is inventory information (like available stock quantity) exposed to functions?

  • If not, what’s the recommended approach to validate inventory before applying discounts or custom validations?

  • Do we need to fetch this information via Admin API/Storefront API separately, or is there a way to include it in the Function input?

Any guidance or best practices around this would be really helpful.

Thanks!

Im pretty positive that it’s not exposed in the Function Input.

With unauthenticated_read_product_inventory you should be able to read it by fetching the product/variant resource for each line item, with the Storefront API.

I think the Admin API is limited depending on what targets and function types you use - but if possible that would probably be your best bet. I’m not 100% on this last part, so might want to test it.

Another option some people use is to simply pass the inventory stock in line item attributes when adding to cart in the storefront. Or keeping metafield updated with current stock, and then making the function read that metafield.

Disclaimer; posted from mobile so didn’t double check docs.