【问题标题】:Tag count automatically changing while clicking on the product tag单击产品标签时标签计数会自动更改
【发布时间】:2018-04-11 07:17:35
【问题描述】:

我正在使用 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_count = 0 %} 应该在循环之外。然后,问题:您的收藏中是否有超过 50 种产品?
  • @AliceGirard,感谢您的回复,不,我只有 20 个产品
  • 我在循环之外添加了 {% assign products_count = 0 %},这次我得到了错误的标签计数。
  • @AliceGirard,还有什么帮助吗?
  • 是的,我的作业当然错了。每次都必须重置,你是对的。对此感到抱歉。关于错误你得到它是正常的。这是因为即使您遍历 collection.all_tags,您的二级循环也会根据您当前的视图进行处理,该视图不包含像 apple 或 Orange 这样的标签。

标签: tags shopify


【解决方案1】:

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

{% assign collection = collections.all %}

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

如果您没有带有all 句柄的集合,您可以通过关注this process 创建一个:

  1. 转到产品 > 产品系列。
  2. 点击添加收藏。
  3. 创建集合:
    1. 将您的收藏命名为 All
    2. 在“条件”部分,选择“根据条件自动选择产品”。
    3. 设置产品条件“产品价格大于$0”。
  4. 保存

编辑:

这解决了单击标签链接时产品数量发生变化的问题:

{% 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 %}

看起来当您使用带有collections/collection_1/tag_1 之类的 URL 的标签进行过滤时,collection.products 也会被所选标签过滤。上面这行看起来有点乱,但似乎返回了全套产品。

【讨论】:

  • 我添加了您的整个代码计数问题已解决,但过滤器无法正常工作。我的意思是,如果我点击任何标签,它也会显示所有产品。
  • 如果我添加这一行 {% assign collection = collections.all %} 并点击过滤标签,那么它也会显示与标签无关的剩余产品。
  • 可能collection 变量与您在页面上引用当前集合的其他代码冲突。我会尝试将其重命名为 all_collections 以查看是否有帮助。另一种想法可能是使用link_to_tag(尽管您的链接看起来应该可以正常工作)。
  • 我添加了下面的代码,当用户点击标签时显示标签相关的产品 {% if current_tags %} {% for tag in product.tags %} {% if current_tags contains tag %} {{ 标签 | link_to_tag: tag }} {% endif %} {% endfor %} {% else %} {% unless product.tags == empty %} {% assign first_tag = product.tags |第一个 %} {{ first_tag | link_to_tag: first_tag }} {% endunless %} {% endif %}
【解决方案2】:

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

{% 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 }} 允许访问任何特定的集合及其属性。

HTH

备注:如果您的收藏品超过 50 件,这将无法正常工作。

【讨论】:

  • 抱歉回复晚了,我试过你的代码,但没有显示我的标签名称。我只是复制并粘贴您的代码。
  • 请务必将此 sn-p 粘贴到您的循环 {% for tag in collection.all_tags %} 中,并将您的计数保持在 {% assign products_count = 0 %} 的位置。
  • 我添加了代码,但仍然没有显示。你能帮我解决我哪里错了吗? {% for tag in collection.all_tags %} {% assign whole_collection = collections[collection.handle] %} {% for product in whole_collection.products %} {% if product.tags contains tag %} {% assign products_count = products_count |加:1 %} {% endif %} {% endfor %} {% endfor %}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2022-01-09
  • 2017-07-20
  • 1970-01-01
  • 2020-08-17
  • 2021-08-26
相关资源
最近更新 更多