Related Products - How to show random products?

New Member
5 0 0

Hey all,

So I followed the step by step instruction on this link: https://help.shopify.com/themes/customization/products/features/recommend-related-products to include related products on my pages... AND IT WORKS!! 

There is a slight issue, and its that the related products featured are all alphabetical, thus I have the same products showing up for every other product I choose. Is there a way to randomize the products in terms of vendor and product type? I don't know if there exists code on liquid which states randomize, but would be great to know.

 

Kind regards,

G

0 Likes
Shopify Expert
9800 91 1554

Liquid doesn't have a randomise function. To do this you'd :

  • grab a larger set of products (so instead of 5, grab 50). 
  • use JavaScript to randomise the set
  • show the first 5 (of whatever you need), purge the rest.

Now whether you grab that 50 directly in that product template is up to you. I tend to do this with an AJAX call so save adding the extra bloat, but use the method that you like.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
3 Likes
New Member
4 0 0

Hi Jason. I am new to shopify. can you please share any example of the code if you have. or if you can share the process flow. I am asking about the Ajax flow. I need to shuffle between 500 products and show only 10 on home page without increasing the page size.
 

0 Likes
New Member
4 0 0

Hi Jason. I am new to shopify. can you please share any example of the code if you have. or if you can share the process flow. I am asking about the Ajax flow. I need to shuffle between 500 products and show only 10 on home page without increasing the page size.

0 Likes
New Member
5 0 0

Hey Jason,

Thank you so much for your prompt reply. I have 100 products already and am yet to add another 1000 products, just working on how to sort my SKUs and all logistic materials before I add all of them. 

Regarding your solution, I have no idea how to alter the Ajax, I don't want to mess it up. I'm learning coding while starting this e-Commerce as well, so I'm very limited with adding new lines. All I learned how to do so far with coding on Shopify is adding new sections and modifying the theme the way I like it (the Look & Feel of the website). 

If you could please guide me, it would be of great help, just as Mark mentioned. 

Thank you so much,

G

0 Likes
New Member
4 0 0

Hi Gthads,
What i did is,
i made a new collection in which made a condition for product more that 1 in invemtory (so as to include all products in my new collection). After that i made a template and inserted this code


      {% layout none %}
{% paginate collections['home-new'].products by 1000 %}
{{ collections['home-new'].products | json }}
{% endpaginate %}
{{ counter }}

 

"home-new" is basically my new collection name.

i assigned this template to a page and at the output got JSON of all the products.
I will include this page name in the shopify ajax. But how to shuffle the json is my question.
Hope JASON will help us with that.


 

0 Likes
New Member
5 0 0

Awesome, will try this and wait for Jason's reply :) 

Thanks guys for your help

0 Likes
Excursionist
33 1 1

i would like to do this for my home page collection i use the Shopify Booster Theme, my url is www.teeparts.com any help or information to achieve this is well appreciated


@Jason wrote:

Liquid doesn't have a randomise function. To do this you'd :

  • grab a larger set of products (so instead of 5, grab 50). 
  • use JavaScript to randomise the set
  • show the first 5 (of whatever you need), purge the rest.

Now whether you grab that 50 directly in that product template is up to you. I tend to do this with an AJAX call so save adding the extra bloat, but use the method that you like.


0 Likes