Our products have a metafield that allows us to set a ‘preorder’ date. In our frontend, we are only showing this value if the stock levels for a given variant reach zero. We also have multiple inventory locations. Our team has requested that we only show this value if the stock levels for a given variant reach zero for a specific location.
As far as I’m aware, inventory locations are not returned by the storefront API, only admin API.
This is ok for a regular product details page - we are using Next.js, so can fetch the additional data using the admin API server-side. But things are a little more complicated when dealing with a collections page; we load more products client-side, so would need a way to get this additional information for each item. The only way I can see of doing this would be to have one or more API calls to the admin API to fetch the missing information, which isn’t ideal for performance reasons.
So I’m wondering if there’s another way – potentially syncing the inventory data to a metafield or similar? I could write a script that periodically refreshes this, but wondering if there’s a better / simpler way.
Any help would be greatly appreciated, thank you in advance.
Hey @sebastienpowell - you are correct on this one for sure. Right now, the recommended option would be to query the admin API (like this on the product variants object and using the location_id value as a query argument filter). You could also just query the variants by collection_id if needed and pull location field value.
I don’t see any big issues with your use case here though! You could definitely build a polling job that queries the admin API, grabs the inventory location/other inventory information and then store it in a metafield that’s readable by the SFAPI (more info here). You’d just need to make sure the metafields you’re creating are exposed on the storefront.
Hope this helps a bit - let me know if I can clarify anything on my end here!
Hey @Alan_G, thank you for the quick reply! I was actually able to come up with a solution that worked using the admin API, but realised after that this is probably not going to be a viable option for production given the rate limits. The query would be executed on each collection page, and assuming a large number of concurrent users, my thinking is we would hit the limit pretty quickly. Is that fair to say?
On that note, I was looking at Shopify Components (https://www.shopify.com/uk/commerce-components), and from what I can gather if we set this up, the rate limit is lifted? (so guessing this could be an option)
Otherwise, sounds like metafields are the way to go.
No worries, happy to help! You’re on point here - if there were a large number of users creating lots of queries, you could potentially run up against rate limits. For Plus Shops, we do sometimes offer rate limit increases. This would have to be requested through our direct support channels though, and I can’t guarantee anything specifically there, but Commerce Components would also be potential option for sure.
You would also need to touch base with Plus Support to see if they can get you onboarded onto CC (there are some caveats and pricing things that I can’t share on the forums here), but they would be able to discuss that possibility with you more in depth for sure.
That said, I think leveraging metafields would be the best option if the above two options don’t work out - let me know if I can clarify anything as always here
@Alan_G that’s great, thank you! I’m going to go ahead with the metafield solution for now. Will revisit this if necessary. Thank you for taking the time to reply, and for your help, greatly appreciated.