Display total number of variant colors a product has. Help (my example in description)

Fds2020
New Member
4 0 0

I want to display the total number of  variant colors a product has.

For example, if a product has 6 different colors I want to display "6 Colors". 

My code (not working):

{% for total_colors in product.options_by_name['Color'].values.size %}
<p>{{ total_colors }}</p>
{% endfor %}

0 Likes

Hey,

Try this out:

{% assign total_colors = 0 %}
  
  {% if product.available and product.variants.size > 1 %}
    {% for option in product.options %}
      {% if option contains 'Color' %}
        {% for variant in product.variants %}
        	{% assign total_colors = total_colors | plus: 1 %}
        {% endfor %}
      {% else %}
      	<small>No Color variants</small>
      	{% break %}
      {% endif %}
    {% endfor %}
  {% endif %}
  
  {% if total_colors > 0 %}<small>Colors available - {{ total_colors }}</small>{% endif %}
◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Using a free theme? Improve it with copy/paste code snippets clicking here.
◦ Looking to hire an experienced developer? Same day response: ezfycode@gmail.com.
0 Likes
singh12
Tourist
6 0 3

Hello!
Where would I put this code?

0 Likes