Ad

Display HTML If Any Item Of Cart Got Specific Tag

- 1 answer

I want to implement two selectboxes in my shopify cart page if the cart contains an item with tag "test1"

my current code

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

anyone can tell me what I'm doing wrong?

Ad

Answer

Please refer below code. you are rendering unnecessary loops. item access product object directly and product can tags.

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

O/P will like below Image

enter image description here

Ad
source: stackoverflow.com
Ad