Forked from zakhardage/Shopify Random Product Order
Last active
February 10, 2025 17:40
-
-
Save eduardonunesp/50fbab3950436b68c916 to your computer and use it in GitHub Desktop.
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
<script type="text/javascript> | |
function fisherYates ( myArray ) { | |
var i = myArray.length, j, temp; | |
if ( i === 0 ) return false; | |
while ( --i ) { | |
j = Math.floor( Math.random() * ( i + 1 ) ); | |
temp = myArray[i]; | |
myArray[i] = myArray[j]; | |
myArray[j] = temp; | |
} | |
} | |
var collection = new Array(); | |
collection[0]="<a href='{{ collection.products.first.url | within: collection }}'><img src='{{ collection.products.first.featured_image | product_img_url: 'large' }}' alt='{{ collection.products.first.title | escape }}' />{{ product.title }}<br />{% if collection.products.first.compare_at_price_min > 0 %}<span class='strike'>{{ collection.products.first.price | money }}</span> <span class='red'>{{ collection.products.first.compare_at_price_min | money }}</span>{% else %}{{ collection.products.first.price | money }}{% endif %}</a>" | |
{% for product in collection.products offset:1 %} | |
collection[{{ forloop.index }}]="<a href='{{ product.url | within: collection }}'><img src='{{ product.featured_image | product_img_url: 'large' }}' alt='{{ product.title | escape }}' />{{ product.title }}<br />{% if product.compare_at_price_min > 0 %}<span class='strike'>{{ product.price | money }}</span> <span class='red'>{{ product.compare_at_price_min | money }}</span>{% else %}{{ product.price | money }}{% endif %}</a>"; | |
{% endfor %} | |
fisherYates(collection); | |
for(var i=0;i<collection.length;i++) { | |
document.write(collection[i]); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment