user3903296
user3903296

Reputation: 199

How do I show a tag to represent multiple products? Shopify Liquid

Hello and thanks for reading my post!

I have a collection with multiple products. On a custom collection template, I want to show the tags only for those that contain multiple products (or when more than 1 product in that collection have the same tag)

I assume it would go something like:

  {% for tag in collection.all_tags %}
    {% if tag.product.size >= 1 %}
      has more than 1 product.  
    {% endif %}
  {% endfor %}

Upvotes: 1

Views: 461

Answers (1)

Steph Sharp
Steph Sharp

Reputation: 11682

I've answered similar questions here and here.

You want something like this:

{% for tag in collection.all_tags %}
    {% assign products_count = 0 %}
    {% for product in collection.products %}
        {% if product.tags contains tag %}
            {% assign products_count = products_count | plus: 1 %}
        {% endif %}
    {% endfor %}

    {% if products_count > 1 %}
        {{ tag }}
    {% endif %}
{% endfor %}

Upvotes: 1

Related Questions