Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I have the following code and i am trying to display a specific product by the value in the Array "products" . Is this possible ?
My goal is to have the opportunity to call a random product by its ID in a specific collection.
Thanks for your help !
Hi @yungmuehle, see if you can make the below Liquid code work,
{% assign collection = collections['TEST'] %}
{% assign specific_product_id = 1234567890 %} <!-- Replace with your specific product ID -->
{% for product in collection.products %}
{% if product.id == specific_product_id %}
<div class="informations">
<a class="ProductName">{{ product.title }}</a>
</div>
{% break %}
{% endif %}
{% endfor %}
Hope it's a good start for you.
Awesome thank you ! But is there a logic behind numbers of the Product ID ? Because i am looking for a range to select them randomly
Okay let's improve the code. Try this,
{% assign collection = collections['TEST'] %}
{% assign product_ids = '1234567890, 9876543210' | split: ', ' %} <!-- Replace with your product IDs -->
{% assign random_index = 'now' | date: '%s' | modulo: product_ids.size %}
{% assign random_product_id = product_ids[random_index] %}
{% for product in collection.products %}
{% if product.id == random_product_id %}
<div class="informations">
<a class="ProductName">{{ product.title }}</a>
</div>
{% break %}
{% endif %}
{% endfor %}
i think .size is wrong because it returns the number of characters in a string or the number of items in an array. Not the value
I am making progress and now it looks like the value for the "random_id" item isnt recognized in the if-loop. I think its because of the format because when i compare it to the actual product ID it works fine.
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025