Instantly share code, notes, and snippets.
Created
May 20, 2021 03:18
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save JoeJHoward/71dc7e2342506741e88b88bdb0e57813 to your computer and use it in GitHub Desktop.
Renders related products from collections the of the current product in random order.
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
{%- liquid | |
assign max_products = 9 | |
assign excluded_collections = 'frontpage,all' | split: ',' | |
assign related_collection_1 = false | |
assign related_collection_2 = false | |
assign related_collection_3 = false | |
assign related_collection_4 = false | |
assign related_collection_5 = false | |
for _collection in product.collections | |
unless excluded_collections contains _collection.handle | |
if related_collection_1 == false | |
assign related_collection_1 = _collection | |
elsif related_collection_2 == false | |
assign related_collection_2 = _collection | |
elsif related_collection_3 == false | |
assign related_collection_3 = _collection | |
elsif related_collection_4 == false | |
assign related_collection_4 = _collection | |
elsif related_collection_5 == false | |
assign related_collection_5 = _collection | |
endif | |
endunless | |
endfor | |
assign product_handles_str = '' | |
assign min = 1 | |
assign max = 10 | |
assign diff = max | minus: min | |
if related_collection_1 | |
for _product in related_collection_1.products | |
assign chance = "now" | date: "%N" | modulo: diff | plus: min | |
if _product.handle == product.handle | |
continue | |
endif | |
if product_handles_str contains _product.handle | |
continue | |
endif | |
if chance >= 5 | |
assign product_handles_str = product_handles_str | append: ',' | append: _product.handle | |
else | |
assign product_handles_str = product_handles_str | prepend: _product.handle | prepend: ',' | |
endif | |
endfor | |
endif | |
if related_collection_2 | |
for _product in related_collection_2.products | |
assign chance = "now" | date: "%N" | modulo: diff | plus: min | |
if _product.handle == product.handle | |
continue | |
endif | |
if product_handles_str contains _product.handle | |
continue | |
endif | |
if chance >= 5 | |
assign product_handles_str = product_handles_str | append: ',' | append: _product.handle | |
else | |
assign product_handles_str = product_handles_str | prepend: _product.handle | prepend: ',' | |
endif | |
endfor | |
endif | |
if related_collection_3 | |
for _product in related_collection_3.products | |
assign chance = "now" | date: "%N" | modulo: diff | plus: min | |
if _product.handle == product.handle | |
continue | |
endif | |
if product_handles_str contains _product.handle | |
continue | |
endif | |
if chance >= 5 | |
assign product_handles_str = product_handles_str | append: ',' | append: _product.handle | |
else | |
assign product_handles_str = product_handles_str | prepend: _product.handle | prepend: ',' | |
endif | |
endfor | |
endif | |
if related_collection_4 | |
for _product in related_collection_4.products | |
assign chance = "now" | date: "%N" | modulo: diff | plus: min | |
if _product.handle == product.handle | |
continue | |
endif | |
if product_handles_str contains _product.handle | |
continue | |
endif | |
if chance >= 5 | |
assign product_handles_str = product_handles_str | append: ',' | append: _product.handle | |
else | |
assign product_handles_str = product_handles_str | prepend: _product.handle | prepend: ',' | |
endif | |
endfor | |
endif | |
if related_collection_5 | |
for _product in related_collection_5.products | |
assign chance = "now" | date: "%N" | modulo: diff | plus: min | |
if _product.handle == product.handle | |
continue | |
endif | |
if product_handles_str contains _product.handle | |
continue | |
endif | |
if chance >= 5 | |
assign product_handles_str = product_handles_str | append: ',' | append: _product.handle | |
else | |
assign product_handles_str = product_handles_str | prepend: _product.handle | prepend: ',' | |
endif | |
endfor | |
endif | |
assign related_products = false | |
if product_handles_str != '' | |
assign first_char = product_handles_str | slice: 0 | |
if first_char == ',' | |
assign product_handles_str = product_handles_str | remove_first: ',' | |
endif | |
assign related_handles_str = '' | |
assign rendered_count = 0 | |
assign product_handles_array = product_handles_str | split: ',' | |
assign min = 1 | |
assign max = product_handles_array | size | |
assign diff = max | minus: min | |
assign loops = 1000 | |
if max <= max_products | |
assign loops = 100 | |
endif | |
for i in (1..loops) | |
assign pluck = "now" | date: "%N" | modulo: diff | plus: min | |
assign _handle = product_handles_array | slice: pluck, 1 | |
if rendered_count >= max_products | |
break | |
endif | |
if related_handles_str contains _handle | |
continue | |
endif | |
assign related_handles_str = related_handles_str | append: ',' | append: _handle | |
assign rendered_count = rendered_count | plus: 1 | |
endfor | |
assign first_char = related_handles_str | slice: 0 | |
if first_char == ',' | |
assign related_handles_str = related_handles_str | remove_first: ',' | |
endif | |
assign related_products = related_handles_str | replace: '[', '' | replace: ']', '' | replace: '"', '' | split: ',' | |
endif | |
-%} | |
{%- if related_products -%} | |
<section> | |
{%- for _handle in related_products -%} | |
{%- assign _product = all_products[_handle] -%} | |
{%- comment -%} DO WHAT YOU WANT HERE WITH "_product" {%- endcomment -%} | |
{%- endfor -%} | |
</section> | |
{%- endif -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment