I used to print the product variants data to the storefront with liquid like this:
variants: {{ product.variants | json }}
Then, I can access a product’s variants with JS. When I check the variant liquid doc, there are inventory_policy and inventory_quantity fields, but they are missing with the JSON filter.
Can you add the missing fields to the variant? Or I must use a forloop to loop through all variants myself? I’m afraid there may be a performance issue, especially when the variants are upgrade to 2,000 within a product. My app have to loop through all products of a collection.
According to my knowledge you have to use a for loop. But there will be performance issue. To reduce that use a for loop to manually construct a JSON array with only the fields you need for each variant. This allows you to reduce the data size and control precisely what gets sent to the storefront.
Yes, I know it’s possible. But 20 - 30 products with 2,000 variants each will loop through 40K - 60K variants. I don’t think it’s a good idea.
I just wonder if the inventory_policy and inventory_quantity fields are exposed on a variant but removed in the json filter. If there is a privacy concern, why doesn’t Shopify remove these fields on a variant?
If you need exact inventory data inside a theme context then you’ll need to access it directly from the variant object. Can you share any additional context on what you’re trying to accomplish?
I’m trying to show specific products where customers can build a bundle, e.g., select any five products from a collection, and get a discount.
A user wants to add a quantity step, e.g. adding 5 variants in a click to build a 5-pack. But I don’t have the inventory quantity on hand. As a result, I can’t disable the add button when the page is loaded. This checking can only be done on our server