Showing Item Counts for Tags

Highlighted
Excursionist
46 0 7

If you have tags showing on a collection page, how can you also show the item count for each tag?

For example, let's say you had tags for colors, you'd want to also show the item count on each tag. Something like this...

Blue (23)
Green (6)
Red (10)

I know how to get the product count for the current collection using {{ collection.products_count }} but I want per tag as well. I've found other articles like this one that suggest looping through the products and have them counted. So far I've not actually gotten those examples to work. I'm wondering if it's something with my approach or theme?
I'm working on the Supply theme and using the Advanced Group Filtering. The code I'm using for rendering the group filters is this:
 

{% for cat_item in cat_array %}
        <div class="grid-item small--one-half medium--one-third">
          <h3>{{ cat_item }}</h3>
          <ul class="advanced-filters">
            {% comment %}
              Loop through collection tags
            {% endcomment %}
            {% for tag in collection.all_tags %}
              {% assign cat = tag | split: '_' | first %}              
              {% if cat != tag and cat_item == cat %}
                {% comment %}
                  Strip out tag category prefix and add/remove link for tag filtering
                {% endcomment %}
                {% if current_tags contains tag %}
                  <li class="advanced-filter active-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_remove_tag: tag }}</li>
                {% else %}
                  <li class="advanced-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_add_tag: tag}}</li>
                {% endif %}
              {% endif %}
            {% endfor %}
          </ul>
        </div>
      {% endfor %}

...So basically I need to work in some additional code that will be looping through the products and counting for each tag?

In addtion, I'd also like to add some logic so that if a tag contains 0 results, then it will hide the tag.

Would love some ideas!

 

0 Likes
Highlighted
Shopify Partner
1366 24 246
{% for tag in collection.all_tags %}
  {% assign tagCount = 0 %}
  {% for product in collection.products %}
    {% for productTag in product.tags %}
      {% if productTag == tag %}
         {% increment tagCount %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% if tagCount > 0 %}
    <p> {{ tag }} {{ tagCount }}</p>
  {% endif %}
{% endfor %}

Loop all tags, loop all products, loop all product tags and see in product tag == initial tag. if so increment a counter.

Haven't tryed the code but may work

https://sections.design tips, tricks & Shopify sections
0 Likes
Highlighted
Excursionist
46 0 7

Hi Mircea, I really appreciate your reply! I'm trying to use your code idea but so far I'm not having success. If I use your code untouched, it will generate a long count of numbers like this: 0 1 2 3 4 5 6 7 8 9 10 11 12 13.

I've tried different ways to integrate the code you posted into my code, but also not having any success.

Any thoughts?

0 Likes
Highlighted
Shopify Partner
1366 24 246
{% for tag in collection.all_tags %}
  {% assign tagCount = 0 %}
  {% for product in collection.products %}
    {% for productTag in product.tags %}
      {% if productTag == tag %}
         {% assign tagCount = tagCount | plus: 1 %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% if tagCount > 0 %}
    <p> {{ tag }} {{ tagCount }}</p>
  {% endif %}
{% endfor %}

My bad, did not test the code first. This will work, tested on a live site:

 

 

https://sections.design tips, tricks & Shopify sections
Highlighted
New Member
3 0 0

Hi 

I am using the following code  to count the number of products in a given collection that have specific tags. My issue is that Shopify limits the number of products for the collection to 50. 

{% for custom_tag in filters %}
{% assign product_count = 0 %}
{% for product in collection.products%}
{% if product.tags contains custom_tag %}
{% assign product_count = product_count | plus: 1 %}
{% endif %}
{% endfor %}
{% endfor %}

This only loops through the first 50 products in the collection. So the count is wrong often.

is there a way i can do this more efficiently and/or loop though all products instead of just 50?

0 Likes
Highlighted
Tourist
10 0 1

no you cant, shopify limits a request to 50 products.

You will need an app to achieve this.

0 Likes