Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to randomize related products in Shopify

Tags:

shopify

I have the standard Shopify theme Minimal. Products are assigned to Collections.

Related Items on each product just show the first 4 items it finds in the related Collections. As there are many items in each collection, a lot of the time there related items are completely the same on 100s of products.

How do I edit the code to randomize the results on Related Products?

like image 340
wibbler Avatar asked Oct 24 '25 18:10

wibbler


2 Answers

Steph's answer is better but there is also this non-javascript (and also not truly random, but I like it anyway) solution that hacks date:

{% assign relatedCollection = collections['related-products'] %}
{% assign index = 'now' | date: '%S' %}
{% assign index = index | times: relatedCollection.products.size %}
{% assign index = index | divided_by: 60 %}
{% for product in relatedCollection.products offset: index %}

  ...

{% endfor %}
like image 143
Rick Davies Avatar answered Oct 26 '25 14:10

Rick Davies


Take a look at this article on the Shopify wiki: Recommend related products to your customers. The section "Find a relevant Collection to recommend products" provides a jQuery script for randomizing the related products shown.

You can output all products from the relevant collection and pick a limited number of products randomly using this jQuery plugin: https://github.com/carolineschnapp/jquery-pick/blob/master/jquery.pick.js

See also: Feature multiple random products on your home page

like image 37
Steph Sharp Avatar answered Oct 26 '25 15:10

Steph Sharp