简体   繁体   English

Shopify 标签商品总数

[英]Shopify Tags total items

In Shopify, how do I show a list of tags followed by the number of products with that tag?在 Shopify 中,如何显示标签列表和带有该标签的产品数量?

Example: Black(12), Blue(10).示例:黑色(12)、蓝色(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>

products_count is an attribute of collection , not tag . products_countcollection一个属性,而不是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:在 Shopify 论坛上查看这些类似的讨论:

Solution by Steph is working. Steph 的解决方案正在发挥作用。 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%}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM