0
votes

I have built this code snippet for my shopify store to display more information in the dropdown field. With the change, the status is now also displayed next to the size selection. Available or Sold Out. Unfortunately I can't get it solved that if the stock quantity on a variant is less than 2, that the system then displays the text. Only 1 available.

 <select id="product-select-{{ product.id }}" name="id" class="main_select">
                  {% 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 }} <span>Verfügbar: {{ variant.inventory_quantity}}</span></option>

                    {% else %}
                      <option disabled="disabled">
                        {{ variant.title }} - {{'products.product.sold_out' | t }}
                      </option>

                    {% endif %}

                  {% endfor %}
                </select>

Current enter image description here

1
if there is one quantity no need to show and if quantity is 0 then it will be out of stock. so what is the benifit of it - Bhavin Solanki
It is more about increasing the sales of the product which is only available once. For example, if size 34 is only available once, customers are more likely to buy the size before it is completely sold out. - p_e_88
I could adjust the code with <span>Verfügbar: {{ variant.inventory_quantity}}, but than it shows the quantity for all available variants, but i would like to show it only to those products, where the quantity is less than two - p_e_88

1 Answers

1
votes

You should be able to add another if statement within the option itself, like this:

<select id="product-select-{{ product.id }}" name="id" class="main_select">
  {% 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 }} <span>Verfügbar {% if variant.inventory_quantity < 2 %}: ONLY 1 AVAILABLE{% endif %}</span>
      </option>

    {% else %}
      <option disabled="disabled">
        {{ variant.title }} - {{'products.product.sold_out' | t }}
      </option>

    {% endif %}

  {% endfor %}
</select>