skip to Main Content

I have a products object:

assign rel_products = collection.products

How can I check for a specific product within this object? By id preferably. I’m doing this check so I can continue in a for loop:

{% if rel_products contains related_product.id %}
    {% continue %}
{% endif %}

This code above isn’t working.

2

Answers


  1. I am not very familiar with liquid but the problem in your code snippet looks like you are assigning the collection to the variable rel_products, so you will have to loop through that and then check the id in each iteration. Possibly something like this?

    {% for product in rel_products  %} 
        {% if product contains 'Id you want to check' %}
            {% continue %}
        {% endif %}
    {% endfor %}
    
    Login or Signup to reply.
  2. I think this might be the solution you needed.

    {% assign rel_products = collection.products %}
    
    {% for product in rel_products %}
          {% if product.id == 12345789 %}   //123456789 is just an example
              <!-- Something you want to do -->
          {% endif %}
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search