Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zakhardage
Last active February 23, 2023 07:47
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zakhardage/5761050 to your computer and use it in GitHub Desktop.
Save zakhardage/5761050 to your computer and use it in GitHub Desktop.
Random Product Order in Shopify
<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();
{% for product in collection.products %}
collection[{{ forloop.index | minus:1 }}]="{% include 'random-product' %}";
{% endfor %}
fisherYates(collection);
var randomCollection = collection[0];
for(var i=1;i<collection.length;i++) {
randomCollection += collection[i];
if ( i && ((i+1) % 3 === 0)) {randomCollection += "<br class='clear product_clear' />";}
}
$('.random-collection').html(randomCollection);
</script>
////////////////////////////////////////////////////////////////////////////////////////////////////
{% comment %}Snippet: random-product.liquid {% endcomment %}
<a href='{{ product.url | within: collection }}'><img src='{{ product.featured_image | product_img_url:'large' }}' alt='{{ product.title | escape }}' /><p>{{ product.title | escape }}</p><p>{{ product.price | money }}</p></a>
@jazt3k
Copy link

jazt3k commented Jul 29, 2018

Hi - How do I output the results ?

@parasakthi
Copy link

hi

@parasakthi
Copy link

hi,
{% assign current_date = 'now' | date: '%d' %}

{%- assign products1 = collections['electrical'].products -%}
{% assign shuffled_products_created = products1 | sort: 'created_at' %}
{% assign seed = current_date | date: "%Y%m%d" %}
{% assign shuffled_products = shuffled_products_created | shuffle: 'now' %}
{% assign product_subset = shuffled_products | slice: 0, 20 %}

{% for product in product_subset %}



  
      <div id="slideeve" class="swiper-slide swipe3 col_blur" style="border:1px solid #ccc;margin-top:0px;margin-right:0px;">
        {%- if product.tags contains "24 Hours Dispatch" -%} 
          <div class="ear_tag" style="position:absolute;left:0;top:0;" >
            <img src='{{ '24hrs_small.png' | asset_url }}' alt="{{ product.title | escape }}" />
          </div>
          {%-endif-%}
            {%- if product.tags contains "48 Hours Dispatch" -%} 
          <div class="ear_tag" style="position:absolute;left:0;top:0;" >
             <img src='{{ '48hrs_icon.png' | asset_url }}' alt="{{ product.title | escape }}" />
          </div>
          {%-endif-%}
        <div class="desgn1" style="height: 178px;">
          <div class="desgn2">
            <a href="{{ product.url }}" style="">
              <img class="img-responsive swiper-lazy centerImage" style="max-width:100px;max-height:120px;" src="{{ product.featured_image | product_img_url: 'medium' }}" alt="{{ product.title | escape  }}" />
            </a>
            
            <a href="{{ product.url }}" style="color:black;text-decoration:none;">
              <h6><b style="font-weight:500;font-size:12px;">{{ product.title | truncate: 25}}</b></h6>
            </a>
            <span><h5 style="color:#3da73a;font-size:12px;font-weight: 700;">{{product.price | money }}</h5></span>
            <span><h6 style="color:#737373;font-size:12px;margin-top: -5px;font-weight: 700;"><del>{{product.compare_at_price | money }}</del></h6></span>
            {% assign percent =  product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price | round %}
            <!-- <h5 style="color:red;font-size:12px;"><b>-{{percent}}%</b></h5> -->
            <br>
          </div>
        </div>
        <div class="swiper-lazy-preloader"></div>
      </div>
   
  {% endfor %}

how to random the product in date base.. it is not working properly..if date change the product will be display the change in random

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment