Shopify Tags total items

五迷三道 提交于 2019-12-09 03:23:22

问题


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>

回答1:


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:

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


来源:https://stackoverflow.com/questions/24031787/shopify-tags-total-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!