Skip to content

Instantly share code, notes, and snippets.

@wickcreative
Last active April 14, 2021 19:12
Show Gist options
  • Select an option

  • Save wickcreative/e345f02d297e63b8b46ea276a9585027 to your computer and use it in GitHub Desktop.

Select an option

Save wickcreative/e345f02d297e63b8b46ea276a9585027 to your computer and use it in GitHub Desktop.
Photo Variant Alt Build
{%- assign option_index = 'option1' -%}
{%- assign image_matrix = '[ ' -%}
{%- assign image_grid__default = '' -%}
{% for option in product.options_with_values %}
{%- assign option_lowercase = option | downcase -%}
{% if option.name == 'color' %}
{% capture option_index %}option{{ forloop.index }}{% endcapture %}
{% break %}
{% endif %}
{% endfor %}
{%- assign variant_start = current_variant[option_index] -%}
{%- assign options_by_name = product.options_by_name['Color'].values -%}
{%- assign option_color_size = options_by_name.size | times: 1 -%}
{% if option_color_size < 1 %}
{%- capture options_default -%}Default Title{%- endcapture -%}
{%- assign options_by_name = options_default | split: ',' -%}
{%- assign option_index = 'option1' -%}
{% endif %}
{% for color_option in options_by_name %}
{% capture image_matrix %}{{ image_matrix }}{ "option_color": "{{ color_option }}", "images":[ {% endcapture %}
{%- assign comma_needed = false -%}
{% for image in product.images %}
{%- assign image_alt_split = image.alt | split: ':' -%}
{%- assign image_alt_color = image_alt_split[0] | strip -%}
{%- assign image_alt_color_all = image_alt_color | split: ',' -%}
{% if image.attached_to_variant? %}
{%- assign variant_check = image.variants[0] -%}
{% if variant_check[option_index] == color_option %}
{% if variant_start == color_option %}
{% capture image_grid__default %}{{ image_grid__default }}{% include 'loop-thumbnail' %}{% endcapture %}
{% endif %}
{% capture image_matrix %}{{ image_matrix }}{% if comma_needed %}, {% endif %}{ "alt": "{{ image.alt | escape }}", "src": "{{ image.src | img_url: product_thumb_size }}" }{% endcapture %}
{%- assign comma_needed = true -%}
{% endif %}
{% elsif image_alt_color_all contains color_option %}
{% if variant_start == color_option %}
{% capture image_grid__default %}{{ image_grid__default }}{% include 'loop-thumbnail' %}{% endcapture %}
{% endif %}
{% capture image_matrix %}{{ image_matrix }}{% if comma_needed %}, {% endif %}{ "alt": "{{ image.alt | escape }}", "src": "{{ image.src | img_url: product_thumb_size }}" }{% endcapture %}
{%- assign comma_needed = true -%}
{% endif %}
{% endfor %}
{% capture image_matrix %}{{ image_matrix }} ] }{% unless forloop.last %},{% endunless %}{% endcapture %}
{% endfor %}
{% capture image_matrix %}{{ image_matrix }} ]{% endcapture %}
<script type="application/json" id="ImageJSON-{{ product.id }}">
{{ image_matrix }}
</script>
{% unless image_grid__default != blank %}
{%- capture image_grid__default -%}{%- assign image = product.featured_image -%}{% include 'loop-thumbnail' %}{%- endcapture -%}
{% endunless %}
{{ image_grid__default }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment