Instantly share code, notes, and snippets.
Last active
August 2, 2017 17:27
-
Save sadiesaurus/e01d8106e168677767b84f7ff4b65471 to your computer and use it in GitHub Desktop.
related products with lazyload for Boundless
This file contains 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 section.settings.show_related_products == true %} | |
<style> | |
h2.section-header__title { | |
text-align: center; | |
} | |
</style> | |
<hr> | |
<div itemscope itemtype="http://schema.org/Product" id="ProductSection-{{ section.id }}" data-section-id="{{ section.id }}" data-section-type="product" data-enable-history-state="true"> | |
{% comment %} | |
Number of related items per row, | |
and number of rows. | |
{% endcomment %} | |
{% assign number_of_related_products_per_row = section.settings.related_grid_num %} | |
{% assign number_of_rows = section.settings.related_grid_row %} | |
{% comment %} | |
Heading. | |
Leave blank if you don't need one. | |
{% endcomment %} | |
{% assign heading = section.settings.related_title %} | |
{% comment %} | |
Set either or both to true, if you want | |
to limit yourself to items with same vendor, and/or type. | |
{% endcomment %} | |
{% assign same_vendor = false %} | |
{% assign same_type = false %} | |
{% comment %} | |
Collections to ignore. | |
Never pick related items from those. | |
{% endcomment %} | |
{% assign exclusions = 'frontpage,all' | split: ',' %} | |
{% comment %} | |
Looking for a relevant collection. | |
{% endcomment %} | |
{% if product.metafields.c_f['Related Products'] %} | |
{% assign collection = collections[product.metafields.c_f['Related Products']] %} | |
{% endif %} | |
{% assign found_a_collection = false %} | |
{% if collection and collection.all_products_count > 1 %} | |
{% unless exclusions contains collection.handle %} | |
{% assign found_a_collection = true %} | |
{% endunless %} | |
{% endif %} | |
{% unless found_a_collection %} | |
{% for c in product.collections %} | |
{% unless exclusions contains c.handle or c.all_products_count < 2 %} | |
{% assign found_a_collection = true %} | |
{% assign collection = c %} | |
{% break %} | |
{% endunless %} | |
{% endfor %} | |
{% endunless %} | |
{% comment %} | |
If we have a relevant collection. | |
{% endcomment %} | |
{% if found_a_collection %} | |
{% assign counter = 0 %} | |
{% assign break_at = number_of_rows | times: number_of_related_products_per_row %} | |
{% assign current_product = product %} | |
{% case number_of_related_products_per_row %} | |
{% when '1' %} | |
{% assign grid_item_width = '' %} | |
{% when '2' %} | |
{% assign grid_item_width = 'large--one-half medium--one-half' %} | |
{% when '3' %} | |
{% assign grid_item_width = 'large--one-third medium--one-half' %} | |
{% when '4' %} | |
{% assign grid_item_width = 'large--one-quarter medium--one-third small--one-half' %} | |
{% when '5' %} | |
{% assign grid_item_width = 'large--one-fifth medium--one-third small--one-half' %} | |
{% when '6' %} | |
{% assign grid_item_width = 'large--one-sixth medium--one-third small--one-half' %} | |
{% else %} | |
{% assign grid_item_width = 'large--one-quarter medium--one-third small--one-half' %} | |
{% endcase %} | |
{% capture related_items %} | |
{% for product in collection.products %} | |
{% unless product.handle == current_product.handle %} | |
{% unless same_vendor and current_product.vendor != product.vendor %} | |
{% unless same_type and current_product.type != product.type %} | |
{% include 'product-grid-width' with product_image_type: section.settings.product_image_type, product_image_size: section.settings.product_image_size %} | |
{% include 'product-grid-item' with product_image_spacing: section.settings.product_image_spacing, vendor_enable: section.settings.vendor_enable %} | |
{% assign counter = counter | plus: 1 %} | |
{% if counter == break_at %} | |
{% break %} | |
{% endif %} | |
{% endunless %} | |
{% endunless %} | |
{% endunless %} | |
{% endfor %} | |
{% endcapture %} | |
{% assign related_items = related_items | trim %} | |
{% unless related_items == blank %} | |
{% unless heading == blank %} | |
<header class="section-header"> | |
<h2 class="section-header__title">{{ heading }}</h1> | |
</header> | |
{% endunless %} | |
<div class="product grid grid--uniform grid--no-gutters"> | |
{{ related_items }} | |
</div> | |
{% endunless %} | |
{% endif %} | |
</div> | |
{% endif %} | |
{% schema %} | |
{ | |
"name": "Related products", | |
"settings": [ | |
{ | |
"type": "checkbox", | |
"id": "show_related_products", | |
"label": "Show related products", | |
"default": false | |
}, | |
{ | |
"id": "related_title", | |
"type": "text", | |
"label": "Section title", | |
"default": "Other fine products" | |
}, | |
{ | |
"type": "select", | |
"id": "product_image_type", | |
"label": "Image style", | |
"options": [ | |
{ | |
"value": "portrait", | |
"label": "Tall/square" | |
}, | |
{ | |
"value": "landscape", | |
"label": "Wide" | |
} | |
] | |
}, | |
{ | |
"type": "select", | |
"id": "product_image_size", | |
"label": "Image size", | |
"default": "medium", | |
"options": [ | |
{ | |
"value": "small", | |
"label": "Small" | |
}, | |
{ | |
"value": "medium", | |
"label": "Medium" | |
}, | |
{ | |
"value": "large", | |
"label": "Large" | |
}, | |
{ | |
"value": "xlarge", | |
"label": "Extra large" | |
} | |
] | |
}, | |
{ | |
"type": "checkbox", | |
"id": "product_image_spacing", | |
"label": "Add spacing between images" | |
}, | |
{ | |
"type": "checkbox", | |
"id": "vendor_enable", | |
"label": "Show product vendor" | |
} | |
] | |
} | |
{% endschema %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment