{%- doc -%} Renders a swatches variant picker, used within the product-swatches block. A swatch will rendered in a pre-selected state if the context allows it. Allowed contexts are search and filtered collections. @param {object} product_resource - The product object, which contains variants and options. @param {boolean} [has_option_selected] - Whether to render the swatches in a pre-selected state. @example {% render 'variant-swatches', product_resource: product %} {%- enddoc -%} {% render 'swatch-styles' %} {% render 'variant-picker-styles' %} {% stylesheet %} swatches-variant-picker-component { display: flex; width: 100%; flex-direction: row; justify-content: var(--product-swatches-alignment-mobile); @media screen and (min-width: 750px) { justify-content: var(--product-swatches-alignment); } --overflow-list-padding-block: calc( var(--product-swatches-padding-block-start) + var(--focus-outline-offset) + var(--focus-outline-width) ) calc(var(--product-swatches-padding-block-end) + var(--focus-outline-offset) + var(--focus-outline-width)); --overflow-list-padding-inline: calc( var(--product-swatches-padding-inline-start) + var(--focus-outline-offset) + (1.5 * var(--focus-outline-width)) ) calc(var(--product-swatches-padding-inline-end) + var(--focus-outline-offset) + var(--focus-outline-width)); overflow-list::part(list) { gap: var(--gap-sm); } } swatches-variant-picker-component overflow-list[disabled='true'] { --overflow-list-padding-block: 0px; margin-block-start: calc( var(--product-swatches-padding-block-start) + var(--focus-outline-offset) + var(--focus-outline-width) ); margin-block-end: calc( var(--product-swatches-padding-block-end) + var(--focus-outline-offset) + var(--focus-outline-width) ); } .hidden-swatches__count { display: flex; align-self: center; align-items: center; justify-content: center; color: rgb(var(--color-foreground-rgb) / var(--opacity-40-60)); background-color: transparent; padding: 0; border: 0; border-radius: 0; &::before { /* This doesn't work in Safari without the counter-reset. https://stackoverflow.com/a/40179718 */ counter-reset: overflow-count var(--overflow-count); content: '+' counter(overflow-count); line-height: 1; cursor: pointer; } } .hidden-swatches__count:hover { color: var(--color-foreground-rgb); } {% endstylesheet %} {% liquid # Allow pre-selection of a swatch if there is a selected variant # product.selected_variant is automatically set in filtered collection, filtered search and some unfiltered search contexts based on result relevance. # Applying a filter updates the displayed image, so a swatch can help communicate the change. assign allow_preselection = false if product_resource.selected_variant or has_option_selected assign allow_preselection = true endif %}
{% comment %} Find the first product option that has swatches. Then render individual swatches with information on the most relevant variant image to be used in: - a data-media-id attribute to show relevant media in a card-gallery on hover/click. - the swatch content, optionally based on theme settings. {% endcomment %} {%- for product_option in product_resource.options_with_values -%} {%- liquid assign swatch_count = product_option.values | map: 'swatch' | compact | size -%} {% if swatch_count == 0 %} {% continue %} {% endif %} {%- liquid # Identify which option position this swatch option is # Use product_option.position which is the actual position among ALL options assign swatch_option_position = product_option.position assign swatch_option_key = 'option' | append: swatch_option_position -%}
{% capture children %} {%- for product_option_value in product_option.values -%} {% liquid comment Identify the most relevant variant image to show in a card-gallery, and optionally as the swatch content. The `product_option_value.variant` can be nil when no valid variant is associated with the combination of the current option-value and the other option values. e.g. a product option Color: "Red" does not exist for the product option Size: "Small". In this case, use the first available variant for links and fallback media. For combined listings, cache the child product reference to avoid resolving `product_option_value.variant.product` multiple times. endcomment assign option_value_variant = product_option_value.variant assign featured_media = option_value_variant.featured_media assign child_product = nil assign first_available_variant = nil assign fallback_featured_media = nil if option_value_variant.available assign first_available_variant = option_value_variant endif if featured_media == blank if product_option_value.product_url assign child_product = option_value_variant.product endif if child_product assign featured_media = child_product.featured_media endif endif # Single pass; stop once variant and media are known. if first_available_variant == blank or featured_media == blank for variant in product_resource.variants if variant.available and variant[swatch_option_key] == product_option_value.name if first_available_variant == blank assign first_available_variant = variant endif if featured_media == blank and fallback_featured_media == blank and variant.featured_media assign fallback_featured_media = variant.featured_media endif if first_available_variant != blank if featured_media != blank or fallback_featured_media != blank break endif endif endif endfor endif if featured_media == blank assign featured_media = fallback_featured_media endif %}
  • {%- endfor -%}
  • {% endcapture %} {% render 'overflow-list', children: children, ref: 'overflowList' %}
    {% comment %} Product cards render only the first option that has swatches. Stop after rendering it; additional option combinations resolve through section rendering. {% endcomment %} {% break %} {%- endfor -%}