我正在使用 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 %}
提前致谢。
看来您缺少的步骤是第一行here:
{% assign collection = collections.all %}
您正在迭代当前集合,因此您已经注意到,当您单击标签时,结果会发生变化。
如果您没有带手柄的收藏all
,您可以通过以下方式创建一个这个流程:
- 转到产品 > 系列。
- 单击添加集合。
- Create the collection:
- 为您的收藏命名
All
.
- 在“条件”部分中,选择“根据条件自动选择产品”。
- 设置产品条件“产品价格大于 0 美元”。
- Save
Edit:
这解决了单击标签链接时产品数量发生变化的问题:
{% 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
then collection.products
也按所选标签进行过滤。上面这行看起来有点乱,但看起来返回的是全套产品。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)