Another bit of my UGLY code

New Member
12 0 0

On a product's page, I want to display four products from the product's collection, starting at a random place in the collection and NOT including the original product.

The attached code - mostly works but I am sure it's pretty UGLY and was wondering if anyone cares to comment or suggest a more elegant code snippet.

The code is in use e.g. HERE

MANY THANKS, Robert.

              
              
{% comment %} RAH - START - code to show FOUR related products from same genre    {% endcomment %}     
{% assign curtitle = product.title %}  
{% assign curgenre = product.metafields.Book-attributes.genre | handle %} 
{% if collections[curgenre].products != '' %}    
    <h2>Some more <a href="http://www.ex-l-ence.com/collections/{{curgenre}}">  
    {% if curgenre == 'biographies' %}
        Biography
    {% else %}
        {{ product.metafields.Book-attributes.genre }} 
    {% endif %}
    </a>titles that you might like...</h2> 
{% endif %}
              
{% comment %} RAH - Check to see if less than 5 books in collection - in which case random selection not needed {% endcomment %}
{% assign = numincol = 0 %}
{% for product in collections[curgenre].products %}              
      {% assign numincol = numincol | plus: 1 %}        
{% endfor %}
{% if numincol > 5 %}              
   {% comment %} RAH - START - Semi random - not perfect - way to select random book or books for related products {% endcomment %}
   {% capture index %}{{ 'now' | date: '%S' | plus: 1 | times: numincol | divided_by: 61 }} {% endcapture %}
   {% comment %} RAH - END - Semi random - not perfect - way to select random book or books for related products {% endcomment %}              
      {% assign index = index | plus: 0 %}
      {% assign maxnumber = numincol | minus: 4 %}
      {% if index > maxnumber %}
         {% assign index = maxnumber | minus: 1 %}
      {% endif %}
      {% assign myforcount = 0 %}
      {% for product in collections[curgenre].products offset:index limit: 5 %}
         {% if product.title != curtitle %}
             {% assign myforcount = myforcount | plus: 1 %}
             {% if myforcount < 5 %}
                {% include 'product-grid-item'%}
             {% endif %}
         {% endif %}
      {% endfor %}
{% else %}
      {% for product in collections[curgenre].products %}
         {% if product.title != curtitle %}
             {% include 'product-grid-item'%}
         {% endif %}
      {% endfor %}
{% endif %}              
{% comment %} RAH - END - code to show related products from same genre    {% endcomment %}               
      

By the way tried originaly using the number of products in collection variable but that seemed to be unreliable ???
 

0 Likes
Shopify Partner
2495 13 462

Hi R!

I would not use 

{% if collections[curgenre].products != '' %}

because .products is not a string but an array, aftert all. I'd use one of

{% if collections[curgenre].products.products_count > 0 %}
{% if collections[curgenre].products.size > 0 %}
{% unless collections[curgenre].empty? %}
{% unless collections[curgenre] == empty %}

I saw your comment about number of products in collection, but my experience differs from yours.

Note that .size will be limited by pagination/limit of products accessible via liquid  (i.e. 50 by default) -- and your loop over products as well, while .products_count show the actual number of products in collection.

This all is IMHO...

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com! My post solved your problem? This is my Paypal too :)
1 Like
Highlighted
Astronaut
1925 0 352

You can try this.  But be warned that I haven't thoroughly tested it.

<!-- Define how many products are to be shown. -->
{% assign number_of_products = 4 %}

<!-- Capture the handle and collection of the current product. -->
{% assign current_handle = product.handle %}
{% assign current_genre = product.metafields.Book-attributes.genre | handle %}
{% assign current_genre = collections[current_genre] %}

<!-- Generate a random number from 0 to (n-1), where n is the number of products in the genre collection. -->
{% assign seed = 'now' | date:'%S' | modulo:current_genre.all_products_count %}

<!-- Check that the genre collection has at least one product. If not, do nothing. -->
{% if current_genre.all_products_count > 0 %}
  <h2>Some more {{ current_genre.title | replace:'Biographies','Biography' | link_to:current_genre.url }} titles that you might like...</h2>

  <!-- Start a count of the number of products that have been picked to show. Stop/Break once we reach the defined limit, number_of_products. -->
  {% assign count = 0 %}
  {% for product in collection.products offset:seed %}
    {% if count >= number_of_products %}{% break %}{% endif %}
    {% if product.handle != current_handle %}
      {% assign count = count | plus:1 %}
      {% include 'product-grid-item' %}
    {% endif %}
  {% endfor %}
  {% for product in collection.products limit:seed %}
    {% if count >= number_of_products %}{% break %}{% endif %}
    {% if product.handle != current_handle %}
      {% assign count = count | plus:1 %}
      {% include 'product-grid-item' %}
    {% endif %}
  {% endfor %}

{% endif %}

 

1 Like