Show particular variant select

Dilby
Shopify Expert
43 0 10
{% for variant in product.variants %}
{% if variant.available %}
<option {% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %} data-sku="{{ variant.sku }}" 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 %}

Hi all -

I'm wanting to show only a certain variant in a dropdown in my theme, and can't work this one out. The original code I have is below, which obviously loops through all available variants. I would like to just show the variant 'model' but am stumped! Thanks

 

 

0 Likes
Matt_Miller
Tourist
31 0 1

Hey Dilby

You would need to use an    IF     statement to determine whether its a certain variant or not. if not, dont add to the dropdown. if yes then add it

  {% assign criteriaVariable = "specific variant or can use a variable" %}

{% if variant_link.title contains criteriaVariable %}

------- here add it to the dropdown

{%endif%}

this is all done inside your    '  for   '  loop so it is applied for each variant retrieved

you can use variables like :  collection.title   if you want to make the variant name to the current collection etc or just a hardcoded value like i have shown in the code.

 

Hope this helps,

Matt

0 Likes
Dilby
Shopify Expert
43 0 10

Thanks so much matt!

0 Likes
Matt_Miller
Tourist
31 0 1

you are most welcome!

glad it helped!

 

Matt

0 Likes
Carlos249
New Member
1 0 0

Hey Dilby,

Did you ever find out how to do this? I trying to do the same thing and need some help.

Cheers,

Carlos.

0 Likes