if quantity 2, i need to hide "add to cart" button and visible "sold out"

Topic summary

A developer is working on a Shopify product page where they need to hide the “Add to Cart” button and display “Sold Out” when inventory reaches a quantity of 2 or less.

Current Status:

  • The “Sold Out” label displays correctly when quantity is 2
  • However, the button itself remains enabled and clickable

Code Implementation:
The developer has shared their Liquid template code showing conditional logic that:

  • Checks if inventory_management is set to ‘shopify’
  • Evaluates inventory_quantity for both single variants and products with multiple variants
  • Changes button text to “Sold Out” when quantity ≤ 2
  • Attempts to disable the button using disabled attributes based on inventory conditions

The Problem:
Despite the conditional logic to disable the button when inventory_quantity <= 2, the “Sold Out” button remains clickable instead of being properly disabled.

The code snippet appears incomplete (reversed/corrupted text at the end), suggesting the developer may need help completing or debugging their button disable functionality.

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

in this product available quantity and on hand qty 2. so “sold out” label show correctly. but i need to disable sold out button.

{%- form 'product', product, id: form_id, class: 'product-single__form' -%}

  {%- if shop.taxes_included or shop.shipping_policy.body != blank -%}
    <div class="product__policies rte">
      {%- if shop.taxes_included -%}
        {{ 'products.product.include_taxes' | t }}
      {%- endif -%}
      {%- if shop.shipping_policy.body != blank -%}
        {{ 'products.product.shipping_policy_html' | t: link: shop.shipping_policy.url }}
      {%- endif -%}
    </div>
  {%- endif -%}

  {%- liquid
    assign enable_dynamic_buttons = false
    if show_dynamic_checkout and template != 'product.preorder'
      assign enable_dynamic_buttons = true
    endif
  -%}

  {%- if enable_dynamic_buttons -%}
    <div class="payment-buttons">
  {%- endif -%}

    {%- liquid
      assign default_text = 'products.product.add_to_cart' | t
      assign button_text = 'products.product.add_to_cart' | t
      if template == 'product.preorder'
        assign default_text = 'products.product.preorder' | t
        assign button_text = 'products.product.preorder' | t
      endif
      if  current_variant.inventory_management == 'shopify'
      if product.variants.size > 1
      **if product.variants.first.inventory_quantity < 2**
       assign button_text = 'products.product.add_to_cart' | t
      endif
      else
       **if current_variant.inventory_quantity <= 2**
       assign button_text = 'products.product.sold_out' | t
        
      endif
      endif
       endif
    
    -%}

    <button
      {% if product.empty? %}type="button"{% else %}type="submit"{% endif %}
      name="add"
      data-add-to-cart
      class="btn btn--full add-to-cart{% if enable_dynamic_buttons and product.selling_plan_groups == empty %} btn--secondary{% endif %}"
        {% if current_variant.inventory_management == 'shopify' %}{%- if product.variants.size > 1 -%}{% if product.variants.first.inventory_quantity >= 2 %} disabled="disabled"{% endif %}{% else %}{% if current_variant.inventory_quantity <= 2 %}disabled="disabled"{% endif %} {% endif %} {% endif %} >
      <span data-add-to-cart-text data-default-text="{{ default_text }}">
        {{ button_text }}
      </span>
    </button>

    {%- if enable_dynamic_buttons -%}
      {{ form | payment_button }}
    {%- endif -%}

  {%- if enable_dynamic_buttons -%}
    </div>
  {%- endif -%}

  <div class="shopify-payment-terms product__policies">{{ form | payment_terms }}</div>

  <select name="id" data-product-select class="product-single__variants no-js">
    {%- for variant in product.variants -%}
      {%- if variant.available -%}
        <option {% if variant == product.selected_or_first_available_variant %}
          selected="selected"{% endif %}
          value="{{ variant.id }}">
          {{ variant.title }} - {{ variant.price | money_with_currency }}
        </option>
      {%- else -%}
        <option disabled="disabled">
          {{ variant.title }} - {{ 'products.product.sold_out' | t }}
        </option>
      {%- endif -%}
    {%- endfor -%}
  </select>

  <textarea data-variant-json class="hide" aria-hidden="true" aria-label="Product JSON">
    {{ product.variants | json }}
  </textarea>
  {%- if product.options.size > 1 -%}
    <textarea data-current-variant-json class="hide" aria-hidden="true" aria-label="Variant JSON">
      {{ current_variant | json }}
    </textarea>
  {%- endif -%}
{%- endform -%}

i need modify this code.