0
votes

I'm trying to do something similar to this question:

Shopify - If item in cart belongs to a specific collection

and I've tried editing the code to suit my needs but it seems to be doubling the output.

What I want is to check to see if any of the items in the cart belong to a particular collection (or check the titles) and then show a related item underneath.

My current code is:

{% for item in cart.items %}

    {% for collection in item.product.collections %}
        {% if collection.handle == "mukluks" %}
            this is a mukluk
        {% endif %}
     {% endfor %}              
{% endfor %}

However that outputs "this is a mukluk" every time there's a match. I'm still trying to figure out how to limit it to just one. Maybe with forloop?

2

2 Answers

1
votes

Your approach is correct, and very similar to what I would have suggested. Alternatively, you could use an if statement with 2 conditions:

{% assign found_mukluk = false %}              
{% for item in cart.items %}
    {% for collection in item.product.collections %}
        {% if found_mukluk == false and collection.handle == "mukluks" %}
            {% assign found_mukluk = true %}
            this is a mukluk
        {% endif %}
     {% endfor %}  
{% endfor %}
0
votes

Well, I ended up figuring this out. I just assigned a variable to false, and then matched it in the loop. Please, if you have a better or more efficient solution, let me know.

{% assign found_mukluk = false %}              
{% for item in cart.items %}


    {% for collection in item.product.collections %}

        {% if collection.handle == "mukluks" %}
            {% assign found_mukluk = true %}
        {% endif %}
     {% endfor %}  

{% endfor %}
{% if found_mukluk %}
          this is a mukluk
{% endif %}