How can I validate and add inventory quantity in liquid code?

Topic summary

A developer is struggling to access and validate inventory_quantity for product variants in Shopify Liquid code.

Core Issue:

  • When attempting to retrieve inventory_quantity in Liquid templates, the field returns no data or is missing from variant objects
  • The code uses conditional logic to display inventory notices based on variant.inventory_quantity and variant.inventory_management

Technical Context:

  • The select dropdown rebuilds when changing variant swatches
  • Attempted to access inventory data via {{ product.variants | json }} but the inventory_quantity field appears to be missing from each variant object
  • Code includes threshold checks (section.settings.inventory_notice_threshold) to conditionally show stock levels

Current Status:
The discussion remains open with no solution provided. Both participants report the same problem accessing inventory quantity data through Liquid, suggesting a potential limitation in how Shopify exposes inventory information to storefront templates.

Summarized with AI on November 17. AI used: claude-sonnet-4-5-20250929.

I need help with this, I have this code in a liquid file:

            <select name="id" class="original-selector" aria-label="{{ 'products.product.selector_label' | t | escape }}">
              {% for variant in product.variants %}
                <option value="{{ variant.id }}"
                      {% if variant == product.selected_or_first_available_variant %} selected="selected"{% endif %}
                      {% if section.settings.show_inventory_notice and variant.inventory_management == 'shopify' and variant.inventory_quantity > 0 %}
                      {% if section.settings.inventory_notice_threshold == 0 or variant.inventory_quantity <= section.settings.inventory_notice_threshold %}
                      data-inventory="{{ variant.inventory_quantity }}"
                      {% endif %}
                      {% endif %}
                      **data-stock="{% if variant.inventory_management == 'shopify' and variant.inventory_quantity <= 0 %}out1{% endif %}"**>{{ variant.title | escape }}</option>

                      <div class="container-inventory_quantity">
                        <p class="intem-inventory_quantity">{% if variant.inventory_management == 'shopify' and variant.inventory_quantity <= 0 %}out1{% endif %}</p>
                      </div>
              {% endfor %}
            </select>

When changing swatch(variant) the select is rebuilt and therefore:

data-stock=“{% if variant.inventory_management == ‘shopify’ and variant.inventory_quantity <= 0 %}out1{% endif %}”

When doing this process it does not get inventory_quantity and I need this to validate and add the out1, any idea how to do it?

The same. I try to get the inventory_quantity of each variant with

variants: {{ product.variants | json }},

But the inventory_quantity field is missed on each variant