skip to Main Content

I am outputing a value based on collection title in shopify

{% for collection in product.collections %}
                          {% if collection.title contains "China" %} 

This is china

{% endif %}
                {% endfor %} 
{% for collection in product.collections %}
                          {% if collection.title contains "China,vietnam" %} 

This is china and vietnam

{% endif %}
                {% endfor %} 

But the issue iam facing is, it is outputing both values which is

1)This is china
2)This is This is china and vietnam

can someone help to check, where the issue is

But the issue iam facing is, it is outputing both values which is

1)This is china
2)This is This is china and vietnam

2

Answers


  1. "China" is a subset of "China,vietnam". The question doesn’t specify the output you expect, but it seems like you should only iterate the products collection once and use two conditions:

    {% for collection in product.collections %}
    {% if collection.title contains "China,vietnam" %} 
    This is china and vietnam
    {% elsif collection.title contains "China" %}
    This is china
    {% endif %}
    
    Login or Signup to reply.
  2. I think you should try this code.

     {% for collection in product.collections %}
    {% if collection.title contains "China" and collection.title contains "vietnam" %} 
            This is china and vietnam
          {% elsif collection.title contains "China" %}
            This is china
            {% else %}
          {% endif %}
        {% endfor %} 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search