Im looking to hide a variant with one option, I have tried using the code below, however I cannot get it to work. Not sure if I’m entering it in the wrong place? (Im using the debut theme)
Okay, as per my understanding, you want to hide the single variant option of color from the specific products wherever applicable. If this is the case then I would request you to provide access to your store as it requires complex code level change.
You can do it by editing the product-template.liquid file. And the code you write will count if there is one variant in product. But you want to hide the variant if it has only one option. To handle this you have to add this code where variant has been place in product-template.liquid file.
{% if option.values.size == 1%} style="display: none;"{% endif %}
In that file you will see code like this.
<div class="selector-wrapper js product-form__item">
<label {% if option.name == 'default' %}class="label--hidden" {% endif %}for="SingleOptionSelector-{{ forloop.index0 }}">
{{ option.name }}
</label>
<select class="single-option-selector single-option-selector-{{ section.id }} product-form__input" id="SingleOptionSelector-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
{% for value in option.values %}
<option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
{% endfor %}
</select>
</div>
Replace that code with below code.
<div class="selector-wrapper js product-form__item">
<label {% if option.name == 'default' %}class="label--hidden" {% endif %}for="SingleOptionSelector-{{ forloop.index0 }}" {% if option.values.size == 1%} style="display: none;"{% endif %}>
{{ option.name }}
</label>
<select class="single-option-selector single-option-selector-{{ section.id }} product-form__input" id="SingleOptionSelector-{{ forloop.index0 }}" data-index="option{{ forloop.index }}" {% if option.values.size == 1%} style="display: none;"{% endif %}>
{% for value in option.values %}
<option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
{% endfor %}
</select>
</div>
Hey, I don’t seem to have product-template.liquid file. I have tried to look at Shopify’s documentation about resolving this issue. But I am exactly not sure where should I make change to make this work?
This was helpful - thank you. I noticed that the correct option still appears - but now hangs out awkwardly to the right of where the hidden element was. Any quick tips on how to fix that?
Once applied, there seems to be a black space where the variant used to appear. This causes other variants (that I want to keep) to be shifted over unnecessarily. Is there a way to alter to code to prevent a “place holder” where the unwanted variant was eliminated?
@simonsprock It’s much better for the user if you use only one dropdown that contains all of the five options. That way the user only has to choose from one dropdown, instead of two separate dropdowns - less friction to get to checkout. You can also fully control the option combinations that way without relying on hacks controlling the dropdown options.
I know this is an old post – are you still having that problem? Make sure you are using display:none and not visibility:hidden. Otherwise, you can provide me with the code and I can try and help you!
I would add something to your answer. Just add the “display:none” spot to the selector-wrapper element. That way you won’t have the blank space problem
Hello, I am trying to apply this to our theme, but I am not finding this line of code in our product-template.liquid. Has this changed in the last few years since this solution was accepted?
I have just started using the free theme CRAFT and have many variants in t-shirts. How can i show just the selected variant’s images and hide others? I do appreciate if you can help me. Thanks in advance