[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Shopify Tags total items

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #9622
    user3706032
    Participant

    In Shopify, how do I show a list of tags followed by the number of products with that tag?

    Example: Black(12), Blue(10).

    Currently the code looks like this, but it doesn’t work.

    <ul>
        {% for tag in collection.all_tags %}
            <li>
                <a href="https://mystore.myshopify.com/collections/all/{{ tag }}">
                    {{ tag }}
                </a>
                ({{ tag.products_count }})
            </li>
        {% endfor %}
    </ul>
    
    #9624
    steph-sharp
    Participant

    products_count is an attribute of collection, not tag.

    I believe you would need to manually loop through the products and count the number with the specified tag.

    For example:

    {% assign collection = collections.all %}
    
    <ul>
        {% 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 %}
    
            <li>
                <a href="https://mystore.myshopify.com/collections/all/{{ tag }}">
                    {{ tag }}
                </a>
                ({{ products_count }})
            </li>
        {% endfor %}
    </ul>
    

    See these similar discussions on the Shopify forums:

    #9623
    ankush-kumar
    Participant

    Solution by Steph is working. But, if i have enabled grouped tags and want to show product count. Kindly let me know solution to append product count in following code:

    '{%comment%} code for color, designer, material sidebar filter {%endcomment%}
    
              {%- for tag in collection.all_tags -%}
                
                {%- assign tag_parts = tag | split: '_' -%}
    
                {%- if tag_parts.size != 2 -%}
                  {%- continue -%}
                {%- endif -%}
    
                {%- assign groups = groups | append: tag_parts.first | append: ',' -%} 
              {% endfor %}
    
    
        {%comment%} End of  code for color, designer, material sidebar filter {%endcomment%}'
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.