Shopify Tags total items

后端 未结 2 1780
北恋
北恋 2021-01-15 06:53

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,

2条回答
  •  时光取名叫无心
    2021-01-15 07:27

    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 %}
    
    
      {% 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 %}
    • {{ tag }} ({{ products_count }})
    • {% endfor %}

    See these similar discussions on the Shopify forums:

    • Show item count associated with a tag
    • Collection Tag Product Count (non-current view)
    • Getting Products By Tags When in a Collection

提交回复
热议问题