Im using the debut theme and im trying to give the user a notification on the product page. the content of the notification depends on whether or not the selected variant is available. this works but only for the variant which is selcted on page load.
This is a rather complex topic involving both backend and frontend development. I’ll try to point you in the right direction.
In short, Liquid code gets rendered on Shopify’s servers before the page is sent to the browser. So on initial page load {{ variant.inventory_quantity }} gets you the inventory quantity of the variant that’s being loaded.
Switching variants doesn’t reload the whole page but rather changes all relevant page elements like price, availability, variant image, etc. on the fly by directly manipulating the DOM. All the product and variant data is typically stored in a JSON object, which is usually the product Liquid object passed through the Liquid json filter. This object however does not include the inventory quantity for each variant (it used to, but that data was removed a couple of years back).
So to get the updated inventory quantity on variant change you would have to add the inventory quantity data to the product JSON object and then hook into the variant change event with Javascript to get the data for the relevant variant from the product JSON object.
alright, it seems like you know what i need this for: overselling. The client wants to oversell products but print out a notification on the product page depending on whether the variant is in stock or not.
And for this i need the inventory quantitiy in the product JSON.
I don’t know of any other way and getting the inventory quantity in the product JSON and displaying a message depending in the selected variant isn’t particularly hard if you know your way around Javascript.