Custom Recommended Products

Highlighted
New Member
1 0 0

I am trying to show custom recommended products at the bottom of the product page .

I created 3 Collections called Red tea, Green tea, and White tea. If the current product is on red-tea show the red tea collection as recommended. It doesn't matter if the product belong on multiple collections it's ok to show just the first match.

{% for collection in product.collections %}
  {% if collection.handle == "red-tea" %}
    {% assign ctm_show = "red-tea" %}
  {% elsif collection.handle == "green-tea" %}
    {% assign ctm_show = "green-tea" %}
  {% else %}
    {% assign ctm_show = "white-tea" %}
  {% endif %}
{% endfor %}r

{% for product in collections[ctm_show].products %}
  {% include 'custom-recommendation.liquid', max_height: max_height %}
{% endfor %}

It is showing ok on green tea products but all others are showing white tea.
red-tea, green-tea, and white-tea are the collection slug. Can anyone tell me what is happening and how to fix this? thanks

EDIT: I tried to output different text to check which of them is firing and I found out that the else block is firing even if one of the conditions were true. 

0 Likes
Highlighted
Shopify Partner
1777 211 370

Just use {% break %} within each conditional to make sure the first match is considered.

 

{% for collection in product.collections %}
  {% if collection.handle == "red-tea" %}
    {% assign ctm_show = "red-tea" %}
{% break %} {% elsif collection.handle == "green-tea" %} {% assign ctm_show = "green-tea" %}
{% break %}
{% else %} {% assign ctm_show = "white-tea" %}
{% break %}
{% endif %} {% endfor %}r {% for product in collections[ctm_show].products %} {% include 'custom-recommendation.liquid', max_height: max_height %} {% endfor %}

 

However, it will still depend on the order of collections the product is part for. If all your products will have 'white-tea' collection first in the list, it might not work the way you want.

 

Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog
0 Likes