Count a products "Color" option?

New Member
6 0 0

Hey Guys,

I'm trying to count how many colors a product comes in. For example I have the following varients of a product:

SIZE     COLOR
S        Black
M        Black
L        Black
S        White
M        White
L        White

If I do a product.variants.size it will return 6. How do I get the count of the color value?

I had tried collecting all the colors into an array, but couldn't figure out anything to make the array unique. This is the closest I got (it doesn't work):

                {% comment %}Collect all colors into a string. This is not unique.{% endcomment %}                
                {% capture colors %}
                    {% for variant in product.variants %}
                        {% for option in variant.options %}
                            {% if product.options[forloop.index0] == 'Color' %}
                                    {% if colors contains option %}
                                    {% else %}
                                       {{ option }}, 
                                    {% endif %} 
                            {% endif %}
                        {% endfor %}
                    {% endfor %}
                {% endcapture %}
                {{ colors.size }}


Thanks,
Drew

0 Likes
Shopify Expert
465 0 35

I would look at Caroline's gist for the color swatches for a starting point

https://gist.github.com/carolineschnapp/3452112

Trevor www.carlowseo.com
0 Likes
New Member
6 0 0

Thanks Carlow for the pointer. I'm close, but still not working. This is what I have so far:

                {% capture colors %}
                {% for option in product.options %}
                 
                    {% assign option_handle = option | handle %}
                	{% assign option_index = forloop.index0 %}
                	 
                	{% assign is_color = false %}
                	{% capture downcased_option %}{{ option | downcase }}{% endcapture %}
                	{% if downcased_option contains 'color' or downcased_option contains 'colour' %}
                		{% assign is_color = true %}
                	{% endif %}
                
                	{% assign values = '' %}
                		{% for variant in product.variants %}
                			{% assign value = variant.options[option_index] %}
                			{% capture wrapped_value %},{{ value }},{% endcapture %}
                			{% unless values contains wrapped_value %}
                
                				{% if is_color %}
                					{{ value }},
                				{% endif %}
                	
                				{% capture values %}{{ values }}{{ wrapped_value }}{% endcapture %}
                			{% endunless %}
                	{% endfor %}
                 
                {% endfor %}
                {% endcapture %}
                
                {% assign colorsArray = colors | split: ',' %}
                {{ colorsArray.size }}             

Now what's strange, is the last two lines. So the varible "color" would be a string that looks like this:

Black,White,

But colorsArray.size is echoing out at 3. It always seems to be 1 too many. Any ideas on why?

Plus, if anyone has any pointers on how to reduce the amount of code required to simply count the color amounts (it would be 4 lines of code in PHP).

Thanks!
 

0 Likes
New Member
1 0 0

 

{% assign num = 0 %} 
{% for option in product.options_with_values %} {% if option.name == 'Color' or option.name == 'color' %} {% for value in option.values %} {% assign num = num | plus: 1 %} {% endfor %} {% endif %} {% endfor %} {{ num }}

This seems too work for me, but in new to liquid so dont know how buggy it is.

0 Likes