Inventory Quantity

Topic summary

A Shopify developer is trying to display inventory quantity warnings when stock is 3 units or less, but the code only shows “SOLD OUT” messages.

Initial Problem:

  • Code using product.variants.first fails to show low stock warnings (≤3 items)
  • Only displays sold out status instead of remaining quantity

Attempted Solutions:

  • BSS-TekLabs suggested using product.selected_or_first_available_variant instead
  • Later provided code to loop through variants and find the first available one with inventory

Current Issue:

  • The proposed solutions pick inventory from the first available variant only
  • Inventory quantity doesn’t update dynamically when users select different product variants
  • The developer needs the display to reflect the selected variant’s inventory, not just the first available one

Status: Unresolved - the core challenge of syncing inventory display with variant selection remains unsolved.

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

I want to show inventory quantity less than or equal to 3 and using this code but it is showing me the sold out only.

{% assign current_variant = product.variants.first %}

<div class="inventoryNote form__label">
{% if current_variant.inventory_quantity > 0 and current_variant.inventory_quantity <= 3 %}
<p style="color: #D0DF00; text-align:center">ONLY {{ current_variant.inventory_quantity }} LEFT</p>
{% elsif current_variant.inventory_quantity <= 0 %}
<p style="color: #D0DF00; text-align:center">SOLD OUT</p>
{% else %}
<!-- Optionally handle cases where inventory is greater than 3 -->
<p style="color: #D0DF00; text-align:center">In stock</p>
{% endif %}
</div>
1 Like
{% assign current_variant = product.selected_or_first_available_variant %}

  {% if current_variant.inventory_management == "shopify" %}
    {% if current_variant.inventory_quantity > 0 and current_variant.inventory_quantity <= 3 %}
      

ONLY {{ current_variant.inventory_quantity }} LEFT

    {% elsif current_variant.inventory_quantity <= 0 %}
      

SOLD OUT

    {% else %}
      

In stock

    {% endif %}
  {% else %}
    
  {% endif %}

Can you try this code @saeedahmed0174

Picking the inventory of first avalaible variant

1 Like
{% assign first_available_variant = null %}

{% for variant in product.variants %}
  {% if variant.inventory_quantity > 0 %}
    {% assign first_available_variant = variant %}
    {% break %}
  {% endif %}
{% endfor %}

  {% if first_available_variant %}
    {% if first_available_variant.inventory_quantity <= 3 %}
      

ONLY {{ first_available_variant.inventory_quantity }} LEFT

    {% else %}
      

In stock

    {% endif %}
  {% else %}
    

SOLD OUT

  {% endif %}

Can you try this code

Not for selected variant

My questions was about this It has same issue picking inventory for first avalaible variant not the selected one

But this code pick the inventory of only first available variant and dono change when i select another varinat