Created
March 7, 2019 13:53
-
-
Save dylanjhunt/1a9ff81dd97d03509d924d4b82d34236 to your computer and use it in GitHub Desktop.
Show all variants as separate products on the collection page on the Brooklyn Theme - https://dylanjh.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if product.variants.size > 1 %} | |
{% for option in product.options %} | |
{% if option == 'Color' %} | |
{% assign index = forloop.index0 %} | |
{% assign colorlist = '' %} | |
{% assign color = '' %} | |
{% for variant in product.variants %} | |
{% capture color %} | |
{{ variant.options[index] }} | |
{% endcapture %} | |
{% unless colorlist contains color %} | |
<!-- /snippets/product-grid-item.liquid --> | |
{% comment %} | |
This snippet is used to showcase each product during the loop, | |
'for product in collection.products' in collection.liquid. | |
A liquid variable (grid_item_width) is set just before the this | |
snippet is included to change the size of the container. | |
Once the variable is set on a page, all future instances of this | |
snippet will use that width. Overwrite the variable to adjust this. | |
Example | |
- assign grid_item_width = 'large--one-quarter medium--one-half' | |
{% endcomment %} | |
{% unless grid_item_width %} | |
{% assign grid_item_width = 'large--one-third medium--one-half' %} | |
{% endunless %} | |
{% unless width %} | |
{%- assign width = 310 -%} | |
{% endunless %} | |
{% unless height %} | |
{%- assign height = 415 -%} | |
{% endunless %} | |
{% assign on_sale = false %} | |
{% if variant.compare_at_price > variant.price %} | |
{% assign on_sale = true %} | |
{% endif %} | |
{% assign sold_out = true %} | |
{% if variant.available %} | |
{% assign sold_out = false %} | |
{% endif %} | |
{% capture img_id_class %}ProductImage-{{ variant.featured_image.id }}{% endcapture %} | |
{% capture img_wrapper_id %}ProductImageWrapper-{{ variant.featured_image.id }}{% endcapture %} | |
{%- assign img_url = variant.featured_image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' -%} | |
<div class="grid__item grid-product {{ grid_item_width }}{% if sold_out %} is-sold-out{% endif %}"> | |
<div class="grid-product__wrapper"> | |
<div class="grid-product__image-wrapper"> | |
<a class="grid-product__image-link" href="{{ variant.url | within: collection }}"> | |
{% if product.featured_image.src == blank %} | |
<img class="grid-product__image" src="{{ variant.image.src | img_url: '1024x' }}" alt="{{ product.featured_image.alt | escape }}"> | |
{% else %} | |
{% include 'image-style' with image: variant.featured_image, small_style: true, width: width, height: height, wrapper_id: img_wrapper_id, img_id_class: img_id_class %} | |
<div id="{{ img_wrapper_id }}" class="product--wrapper"> | |
<div style="padding-top:{{ 1 | divided_by: variant.featured_image.aspect_ratio | times: 100 }}%;"> | |
<img class="product--image lazyload {{ img_id_class }}" | |
src="{{ variant.featured_image | img_url: '150x150' }}" | |
data-src="{{ img_url }}" | |
data-widths="[180, 370, 590, 740, 900, 1080, 1296, 1512, 1728, 2048]" | |
data-aspectratio="{{ product.featured_image.aspect_ratio }}" | |
data-sizes="auto" | |
alt="{{ variant.featured_image.alt | escape }}"> | |
</div> | |
</div> | |
<noscript> | |
<img class="grid-product__image" src="{{ variant.featured_image.src | img_url: '1024x' }}" alt="{{ product.featured_image.alt | escape }}"> | |
</noscript> | |
{% endif %} | |
</a> | |
{% if sold_out %} | |
<div class="grid-product__sold-out"> | |
<p>{{ 'products.product.sold_out_html' | t }}</p> | |
</div> | |
{% elsif on_sale %} | |
<div class="grid-product__on-sale"> | |
{% capture saved_amount %}{{ variant.compare_at_price | minus: variant.price | money_without_trailing_zeros }}{% endcapture %} | |
<p>{{ 'products.general.save_html' | t: saved_amount: saved_amount }}</p> | |
</div> | |
{% endif %} | |
</div> | |
<a href="{{ variant.url | within: collection }}" class="grid-product__meta"> | |
<span class="grid-product__title">{{ product.title }} - {{ variant.title }}</span> | |
<span class="grid-product__price-wrap"> | |
<span class="long-dash">—</span> | |
<span class="grid-product__price"> | |
{% if on_sale %} | |
<span class="visually-hidden">{{ 'products.general.sale_price' | t }}</span> | |
{% else %} | |
<span class="visually-hidden">{{ 'products.general.regular_price' | t }}</span> | |
{% endif %} | |
{{ variant.price | money_without_trailing_zeros }} | |
</span> | |
</span> | |
{% if section.settings.product_vendor_enable %} | |
<p class="grid-product__vendor">{{ product.vendor }}</p> | |
{% endif %} | |
</a> | |
</div> | |
</div> | |
{% capture tempList %} | |
{{colorlist | append: color | append: " " }} | |
{% endcapture %} | |
{% assign colorlist = tempList %} | |
{% endunless %} | |
{% endfor %} | |
{% endif %} | |
{% endfor %} | |
{% else %} | |
{% unless grid_item_width %} | |
{% assign grid_item_width = 'large--one-third medium--one-half' %} | |
{% endunless %} | |
{% unless width %} | |
{%- assign width = 310 -%} | |
{% endunless %} | |
{% unless height %} | |
{%- assign height = 415 -%} | |
{% endunless %} | |
{% assign on_sale = false %} | |
{% if product.compare_at_price > product.price %} | |
{% assign on_sale = true %} | |
{% endif %} | |
{% assign sold_out = true %} | |
{% if product.available %} | |
{% assign sold_out = false %} | |
{% endif %} | |
{% capture img_id_class %}ProductImage-{{ product.featured_image.id }}{% endcapture %} | |
{% capture img_wrapper_id %}ProductImageWrapper-{{ product.featured_image.id }}{% endcapture %} | |
{%- assign img_url = product.featured_image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' -%} | |
<div class="grid__item grid-product {{ grid_item_width }}{% if sold_out %} is-sold-out{% endif %}"> | |
<div class="grid-product__wrapper"> | |
<div class="grid-product__image-wrapper"> | |
<a class="grid-product__image-link" href="{{ product.url | within: collection }}"> | |
{% if product.featured_image.src == blank %} | |
<img class="grid-product__image" src="{{ product.featured_image.src | img_url: '1024x' }}" alt="{{ product.featured_image.alt | escape }}"> | |
{% else %} | |
{% include 'image-style' with image: product.featured_image, small_style: true, width: width, height: height, wrapper_id: img_wrapper_id, img_id_class: img_id_class %} | |
<div id="{{ img_wrapper_id }}" class="product--wrapper"> | |
<div style="padding-top:{{ 1 | divided_by: product.featured_image.aspect_ratio | times: 100 }}%;"> | |
<img class="product--image lazyload {{ img_id_class }}" | |
src="{{ product.featured_image | img_url: '150x150' }}" | |
data-src="{{ img_url }}" | |
data-widths="[180, 370, 590, 740, 900, 1080, 1296, 1512, 1728, 2048]" | |
data-aspectratio="{{ product.featured_image.aspect_ratio }}" | |
data-sizes="auto" | |
alt="{{ product.featured_image.alt | escape }}"> | |
</div> | |
</div> | |
<noscript> | |
<img class="grid-product__image" src="{{ product.featured_image.src | img_url: '1024x' }}" alt="{{ product.featured_image.alt | escape }}"> | |
</noscript> | |
{% endif %} | |
</a> | |
{% if sold_out %} | |
<div class="grid-product__sold-out"> | |
<p>{{ 'products.product.sold_out_html' | t }}</p> | |
</div> | |
{% elsif on_sale %} | |
<div class="grid-product__on-sale"> | |
{% capture saved_amount %}{{ product.compare_at_price | minus: product.price | money_without_trailing_zeros }}{% endcapture %} | |
<p>{{ 'products.general.save_html' | t: saved_amount: saved_amount }}</p> | |
</div> | |
{% endif %} | |
</div> | |
<a href="{{ product.url | within: collection }}" class="grid-product__meta"> | |
<span class="grid-product__title">{{ product.title }} - {{ variant.title }}</span> | |
<span class="grid-product__price-wrap"> | |
<span class="long-dash">—</span> | |
<span class="grid-product__price"> | |
{% if on_sale %} | |
<span class="visually-hidden">{{ 'products.general.sale_price' | t }}</span> | |
{% else %} | |
<span class="visually-hidden">{{ 'products.general.regular_price' | t }}</span> | |
{% endif %} | |
{% if product.price_varies %} | |
{{ product.price_min | money_without_trailing_zeros }} | |
<span class="icon-fallback-text"> | |
<span class="icon icon-plus grid-product__price-min" aria-hidden="true"></span> | |
<span class="fallback-text">+</span> | |
</span> | |
{% else %} | |
{{ product.price | money_without_trailing_zeros }} | |
{% endif %} | |
</span> | |
</span> | |
{% if section.settings.product_vendor_enable %} | |
<p class="grid-product__vendor">{{ product.vendor }}</p> | |
{% endif %} | |
</a> | |
</div> | |
</div> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment