单击产品标签时标签计数会自动更改
Tag count automatically changing while clicking on the product tag
我正在使用 Shopify。我在 collection 页面中,我正在获取所有带有标签计数的过滤器,例如
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 %}
提前致谢。
正如我在评论中所说,问题来自您的辅助循环:
{% for product in collection.products %}
只能访问当前视图,不能访问完整的 collection 产品。
我没有测试过,但我想值得一试:
{% 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 }} 允许访问任何特定的 collection 及其属性。
HTH
备注:如果您的 collection 有超过 50 个项目,这将无法准确工作。
看起来您缺少的步骤是第一行 here:
{% assign collection = collections.all %}
您正在迭代当前 collection,因此正如您注意到的,当您点击标签时结果会发生变化。
如果您没有带句柄 all
的 collection,您可以通过以下 this process:
创建一个
- 转到产品 > Collections。
- 单击添加 collection。
- 创建 collection:
- 给你的 collection 标题
All
。
- 在条件部分,select "Automatically select products based on conditions"。
- 设置产品条件"Product price is greater than [=54=]"。
- 保存
编辑:
这解决了点击标签时产品数量发生变化的问题 link:
{% 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 %}
看起来当您使用 URL 标签过滤时 collections/collection_1/tag_1
然后 collection.products
也被 selected 标签过滤。上面这行看起来有点乱,不过看起来return全套产品。
我正在使用 Shopify。我在 collection 页面中,我正在获取所有带有标签计数的过滤器,例如
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 %}
提前致谢。
正如我在评论中所说,问题来自您的辅助循环:
{% for product in collection.products %}
只能访问当前视图,不能访问完整的 collection 产品。
我没有测试过,但我想值得一试:
{% 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 }} 允许访问任何特定的 collection 及其属性。
HTH
备注:如果您的 collection 有超过 50 个项目,这将无法准确工作。
看起来您缺少的步骤是第一行 here:
{% assign collection = collections.all %}
您正在迭代当前 collection,因此正如您注意到的,当您点击标签时结果会发生变化。
如果您没有带句柄 all
的 collection,您可以通过以下 this process:
- 转到产品 > Collections。
- 单击添加 collection。
- 创建 collection:
- 给你的 collection 标题
All
。 - 在条件部分,select "Automatically select products based on conditions"。
- 设置产品条件"Product price is greater than [=54=]"。
- 给你的 collection 标题
- 保存
编辑:
这解决了点击标签时产品数量发生变化的问题 link:
{% 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 %}
看起来当您使用 URL 标签过滤时 collections/collection_1/tag_1
然后 collection.products
也被 selected 标签过滤。上面这行看起来有点乱,不过看起来return全套产品。