Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Last active June 21, 2021 20:45
Show Gist options
  • Save carolineschnapp/6082654 to your computer and use it in GitHub Desktop.
Save carolineschnapp/6082654 to your computer and use it in GitHub Desktop.
Script that checks if a product has a Colo(u)r option and is available in more than 1 color.
{% assign has_color = false %}
{% assign option_index = 0 %}
{% for option in product.options %}
{% capture downcased_option %}{{ option | downcase }}{% endcapture %}
{% if downcased_option contains 'color' or downcased_option contains 'colour' %}
{% assign option_index = forloop.index0 %}
{% assign has_color = true %}
{% endif %}
{% endfor %}
{% if has_color %}
{% assign how_many_colors = 0 %}
{% assign colors = '' %}
{% for variant in product.variants %}
{% assign color = variant.options[option_index] | append: ',' %}
{% unless colors contains color %}
{% assign colors = colors | append: color %}
{% assign how_many_colors = how_many_colors | plus: 1 %}
{% endunless %}
{% endfor %}
{% if how_many_colors > 1 %}
<p>Available in more colors</p>
{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment