Tags 单击产品标签时,标签计数会自动更改

Tags 单击产品标签时,标签计数会自动更改,tags,shopify,Tags,Shopify,我正在使用Shopify。我在收集页面中,我得到了所有带有标记计数的过滤器 All Products Apple(4) Banana(2) Orange(1) Mango(8) 现在,当我点击任何标签时(例如,我点击了香蕉),它将显示香蕉产品 现在我的问题是点击标签,它改变了标签数量 All Products Apple(0) Banana(2) Orange(0) Mango(4) 我正在使用下面的代码 {% for tag in collection.all_tags %} {% a

我正在使用Shopify。我在收集页面中,我得到了所有带有标记计数的过滤器

All Products
Apple(4)
Banana(2)
Orange(1)
Mango(8)
现在,当我点击任何标签时(例如,我点击了香蕉),它将显示香蕉产品

现在我的问题是点击标签,它改变了标签数量

All Products
Apple(0)
Banana(2)
Orange(0)
Mango(4)
我正在使用下面的代码

 {% 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 %}
 <a class="filter__link" href="/collections/{% if collection.handle != blank %}{{ collection.handle }}{% else %}all{% endif %}/{{ tag | handleize }}"{% if current_tags contains tag %} selected="selected" id="tag_active"{% endif %}>{{ tag }} ({{products_count }})</a>
  {% endfor %}
{%用于集合中的标记。所有_标记%}
{%assign products\u count=0%}
{%用于集合中的产品。产品%}
{%如果product.tags包含标记%}
{%assign products_count=products_count |加:1%}
{%endif%}
{%endfor%}
{%endfor%}

提前感谢。

正如我在评论中所说,问题来自您的二次循环:

{% for product in collection.products %}
它只访问当前视图,而不访问完整集合产品

我没有测试过,但我想值得一试:

{% assign whole_collection = collections[collection.handle] %}
{% for product in whole_collection.products %}
 {% if product.tags contains tag %}
 {% assign products_count = products_count | plus: 1 %}
 {% endif %}
{% endfor %}
说明,类似这样的代码{{collections['the-handle'].url}}允许访问任何特定集合及其属性


备注:如果您的收藏超过50项,则无法准确执行此操作。

看起来您缺少的步骤是第一行:

您正在对当前集合进行迭代,因此,正如您在单击标记时所注意到的,结果会发生变化

如果没有句柄为
all
的集合,可以通过以下方式创建一个集合:

  • 转到产品>收藏
  • 单击“添加集合”
  • 创建集合:
  • 为您的收藏指定标题
    All
  • 在条件部分,选择“根据条件自动选择产品”
  • 设置产品条件“产品价格大于$0”
  • 拯救
  • 编辑:

    这解决了当您单击标记链接时产品计数发生变化的问题:

    {% for tag in collection.all_tags %}
        {% assign products_count = 0 %}
        {% for product in collections[collection.handle].products %}
            {% if product.tags contains tag %}
                {% assign products_count = products_count | plus: 1 %}
            {% endif %}
        {% endfor %}
        <a class="filter__link" href="/collections/{% if collection.handle != blank %}{{ collection.handle }}{% else %}all{% endif %}/{{ tag | handleize }}"{% if current_tags contains tag %} selected="selected" id="tag_active"{% endif %}>{{ tag }} ({{products_count }})</a>
    {% endfor %}
    

    当您使用类似
    collections/collection\u 1/tag\u 1
    的URL按标记筛选时,会出现这种情况,然后
    collection。产品也会按所选标记筛选。上面的行看起来有点凌乱,但它似乎返回了完整的产品集。

    您的第一次分配{%assign products\u count=0%}应该在循环之外。然后,问题:你的收藏中有超过50种产品吗?@Aliegirard,谢谢你的回答,没有,我只有20种产品是在循环之外添加的{%assign products\u count=0%},这次我的标签计数错误。@Aliegirard,这方面还有什么帮助吗?是的,当然是我的错误。每次都必须重置,你是对的。对不起。关于你得到的错误,这是正常的。这是因为即使您循环使用collection.all_标记,您的第二个循环也会根据当前视图进行处理,该视图不包含apple或Orange等标记。很抱歉,回复太晚,我尝试了您的代码,但它没有显示我的标记名。我只是复制并粘贴您的代码。请确保将此代码段粘贴到循环{%for tag in collection.all_tags%}中,并将您的计数保持在{%assign products_count=0%}的位置。我添加了代码,但它仍然没有显示它。你能帮我找出哪里错了吗?{%for collection.all_tags%}{%assign whole_collection=collections[collection.handle]}{%for whole_collection.products%}{%if product.tags包含tag%}{%assign products_count=products_count | plus:1%}{%endif%}{%endfor%}{%endfor%}{%endfor%}我添加了您的整个代码计数问题已得到解决,但过滤器不工作。我的意思是,如果我点击任何标签,它也会显示所有的产品。我添加了这一行,如果我添加这一行{%assign collection=collections.all%}然后单击filter tag,它还会显示与标记无关的剩余产品。
    集合
    变量可能与页面上引用当前集合的其他代码冲突。我会尝试将其重命名为类似于
    all\u collections
    的名称,看看这是否有帮助。另一个想法是使用(尽管您的链接看起来应该可以正常工作)。我添加了下面的代码,以便在用户单击标记{%if-current_-tags%}{%for-tag in-product.tags%}{%if-current_-tags包含标记%}{%endif%}{%endfor%}{%else%}时显示与标记相关的产品{%inspect product.tags==empty%}{%assign first_tag=product.tags | first%}{%endinspect%}{%endif%}
    {% for tag in collection.all_tags %}
        {% assign products_count = 0 %}
        {% for product in collections[collection.handle].products %}
            {% if product.tags contains tag %}
                {% assign products_count = products_count | plus: 1 %}
            {% endif %}
        {% endfor %}
        <a class="filter__link" href="/collections/{% if collection.handle != blank %}{{ collection.handle }}{% else %}all{% endif %}/{{ tag | handleize }}"{% if current_tags contains tag %} selected="selected" id="tag_active"{% endif %}>{{ tag }} ({{products_count }})</a>
    {% endfor %}
    
    {% for product in collections[collection.handle].products %}