See if a product has variants

Nate_Schaub
New Member
4 0 0

I have products listed on a particular collection page, and I'm using the following link to bypass the product detail and shopping cart pages and push customers directly into the checkout process:

<a href="https://mysite.com/cart/{{ product.variants.first.id }}:1">

However, some of our products now have variants, so for those, I want to send them to the cart page to select variants (i.e. shirt size). How do I check to see if a product has variants associated with it? Right now I'm using the code below but it's not working:

  {% if variant.available == true %}
  <a href="{{ product.url | within: collection }}">
  {% else %}
  <a href="https://mysite.com/cart/{{ product.variants.first.id }}:1">
  {% endif %}

Replies 4 (4)
Jason
Shopify Expert
10438 168 2081

Hey Nate,

You can check how many variants the product has.

{% if product.variants.size > 1 %}
  I have more than one default variant
{% else %}
  I have just the one default variant
{% endif %}

Now have variants and have having available variants are two different things. Did you need to check both?

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.
Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.
★ http://freakdesign.com.au ★
Nate_Schaub
New Member
4 0 0

Sweet...thanks Jason. At that point I just need to know if a particular product has any variants in order to push them to the product page to select a variant, then proceed with checkout. On the product page, if a particular variant (i.e. a shirt size) is out of stock, will that variant simply not show up as an option?

AvidBrio
Shopify Expert
231 12 18

@Jason  @Nate_Schaub 

we can check the product has variant

 {% if product.has_only_default_variant %}
      <input id="remove_the_dis_cart_btn" name="id" value="{{ variant.id }}" type="hidden">
    {% else %}
      <select name="id" id="productSelect-{{ section.id }}" class="product-variants__ product-variants-{{ section.id }}">
        <option selected="selected" data-var-price=" {{ product.price_min | money_with_currency }} " > Select </option>
        {% for variant in product.variants %}
          {% if variant.available %}
          
            <option   data-sku="{{ variant.sku }}" data-var-price="{{ variant.price | money_with_currency }}" value="{{ variant.id }}"> {{ variant.title }} {%- comment -%}    - {{ variant.price | money_with_currency }} {%- endcomment -%} </option>

          {% else %}
          <option> Select </option>
            <option disabled="disabled">
              {{ variant.title }} - {{ 'products.product.sold_out' | t }}
            </option>
          {% endif %}
        {% endfor %}
      </select>
      {% endif %}
If you find our comment helpful, hit the like button and accept it as a solution.
Want us to implement custom changes in your store? Contact us
Email me directly - jim@avidbrio.com
magedesigner
New Member
1 0 1

@AvidBrio  

Thanks for providing the solution it's working for me.
i was also worked with you you are really good experience in Shopify