Show one option size when only one variant is available

Highlighted
Tourist
6 0 2

Hi guys,

 

I have the following code in my product page, I want to show the select option size if there is one variant available and not when there are 2 or more variants.

  {% if product.options.size > 1 %}
    <div class="select" {% if product.variants.size == 1 %}style="display: none;"{% endif %}>
      <select id="product-select-{{ product.id }}" name='id'>
        {% for variant in product.variants %}
          <option value="{{ variant.id }}" {% if variant == current_variant %}selected="selected"{% endif %}>
            {{ variant.title }} - {{ variant.price | money }}
          </option>
        {% endfor %}
      </select>
    </div>
    {% if section.settings.product_variants == 'swatches' %}
      {% for option in product.options_with_values %}
        {% include "swatch" %}
      {% endfor %}
    {% endif %}
  {% elsif product.options.size == 1 and product.variants.size > 1 %}
    <div class="select">
      <label>{{ product.options[0] }}</label>
      <select id="product-select-{{ product.id }}" name='id'>
        {% for variant in product.variants %}
          <option data-sku="{{ variant.sku }}" value="{{ variant.id }}" {% if variant == current_variant %}selected="selected"{% endif %}>
            {{ variant.title }} - {{ variant.price | money }}
          </option>
        {% endfor %}
      </select>
    </div>
    {% if section.settings.product_variants == 'swatches' %}
      {% for option in product.options_with_values %}
        {% include 'swatch' %}
      {% endfor %}
    {% endif %}
  {% elsif product.variants.size == 1 %}
    <select id="product-select-{{ product.id }}" name="id" {% if product.variants.size == 1 %}style="display: none;"{% endif %}>
      {% for variant in product.variants %}
        <option value="{{ variant.id }}" {% if variant == current_variant %}selected="selected"{% endif %}>
          {{ variant.title }} - {{ variant.price | money }}
        </option>
      {% endfor %}
    </select>
  {% else %}
    <div class="what-is-it">
      {% if product.options.first != 'Title' %}
        <label>{{ product.options.first }}:</label>
        {% for variant in product.variants %}
          <span class="it-is">{{ variant.option1 | escape }}</span>
        {% endfor %}
      {% endif %}
    </div>
    <div class="product-variants"></div><!-- product variants -->
    <input  type="hidden" id="{{ variant.id }}" name="id" data-sku="{{ variant.sku }}" value="{{ product.variants[0].id }}" />
  {% endif %}

I've tried to change 

  {% if product.options.size > 1 %}

to 

  {% if product.options.size == 1 %}

It worked but the problem is for products that don't have any variants it shows "Default Title" and I don't want to show that.

 

Any ideas?

 

Thanks in advance!

0 Likes
Highlighted

Hello 

Replace that line with: 

{% if product.options.size == 1  and product.options.size > 0 %}
Want to modify or incorporate custom changes on store, Hire us
If helpful then please Like and Accept Solution .
Email: pallavi@oscprofessionals.com
Skype : oscprofessionals-87
Whats app
Free Website Review and advice for sale from Shopify Conversion Experts | Shopify Private App | Custom Theme Development | Custom Modifications In To Shopify Theme | Shopify SEO ; Digital Marketing & amp | Site Speed Optimization | Performance Site Audit
0 Likes
Highlighted
Tourist
6 0 2

Yes but it shows "Default Title" for products that don't have any variant option.

0 Likes
Highlighted
Shopify Expert
1381 111 200

Please Share a Store url to better assist

0 Likes