【问题标题】:Shopify: How would I see if a product in my cart had a specific tagShopify:如何查看我的购物车中的产品是否有特定标签
【发布时间】:2023-12-18 15:26:02
【问题描述】:

我想检查我的结账购物车中的某件商品是否属于收藏品(或者它是否具有特定标签)。

这是我尝试检查是否有产品的“尝试”

{% for item in cart.items %}    
{% if item.product.collections == "SampleProduct" %}
  <p>Enjoy your Product</p>
{% endif %}
{% endfor %}

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    如果你想检查你的产品是否在特定的集合中,你需要这样的东西:

    {% for item in cart.items %}  
      {% assign found_collection = false %}
      {% for collection in item.product.collections %}
        {% if found_collection == false and collection.title == 'SampleProduct' %}
          {% assign found_collection = true %}
        {% endif %}
      {% endfor %}
      {% if found_collection %}
        <p>Enjoy your Product</p>
      {% endif %}
    {% endfor %}
    

    或者如果你想检查一个标签,使用这个:

    {% for item in cart.items %}
      {% if item.product.tags contains 'SampleProduct' %}
        <p>Enjoy your Product</p>
      {% endif %}
    {% endfor %}
    

    有关详细信息,请参阅 contains operator 的 Shopify wiki 页面。

    【讨论】:

    • 非常感谢您的帮助!
    最近更新 更多