【问题标题】:Display HTML if any item of cart got specific tag如果购物车的任何物品有特定标签,则显示 HTML
【发布时间】:2019-11-26 20:55:29
【问题描述】:

如果购物车包含带有标签“test1”的商品,我想在我的购物车页面中实现两个选择框

我当前的代码

{% for item in cart.items %}
  {% for products in item.products %}
      {% for tag in product.tags %}
          {% if tag contains 'test1' %}
            <select>....</select>
          {% endif %}
      {% endfor %}
  {% endfor %}
{% endfor %}

谁能告诉我我做错了什么?

【问题讨论】:

  • 也就是说,一个选择框就在那里?每个 都是一个选择框。

标签: shopify liquid shopify-template


【解决方案1】:

请参考以下代码。您正在渲染不必要的循环。 item 直接访问 product 对象和 product can 标签。

{% for item in cart.items %}
    {% if item.product.tags contains 'custom-tag' %}
         <h1> Yahoo </h1>
    {% endif %}
{% endfor %}

O/P 会喜欢下面的图片

【讨论】:

  • 这可能会显示同一个选择的多个版本。也许在那里添加一个 {% break %}?
  • 所有标签都应该并且将是唯一的,因此在 if 条件下控制只会进行一次。此代码检查购物车中的每个项目是否有特定标签,如果发现它会呈现您在 IF 条件下编写的 HTML。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多