Retrieve a number of randomly-chosen products in Shopify
I need a solution for retrieving a number of randomly-chosen products on my homepage (or any pages). I read in here: Featuring a number of randomly-chosen products on your homepage. But I think It’s not good for me. I continue reading an article here: Featuring one randomly-chosen product on your homepage. I think it’s a good solution. Really this solution supports retrieving only a product, but I can get more if I change the variant for “limit”. Good…. But after that I found an issue with it.
Example: That collection has 10 products and I want to retrieve 5 products. All are OK if the index number is 1 or 2 or 3 or 4 or 5 but if the index number is greater than 5 We will miss some products.
I’m thinking… and after I have solved this issue. Sharing with everybody.
Get index number:
Count product missing
Now we can show products
If the current index doesn’t take up enough of the products. We need add below code: