0
votes

I want to hide the cart button when there are products with the Pre-order product tag and the Regular product tag in the cart.

This code, we were able to get the tags for all products in the cart.

{% for item in cart.items %}
    {% assign carttag = item.product.tags %}                
    <p>{{ carttag }}</p>
{% endfor %}

However, the following code does not work.

  {% if carttag contains 'Preorder' and carttag contains 'Regular' %}
    <p>Stop!</p>               
  {% else %}
     <input type="submit" name="checkout"> 
  {% endif %}

How can I handle all looping values ​​as one?

1

1 Answers

0
votes

You are on the right path, but your code has 2 problems.

  1. You are overriding Cart Tags value for each product. So, you always have Product tags for last product in your cart.
  2. product.tags return Array while you are treating it as string.

Fixing these issues your code will look like

{% assign cartTags = "" %}

{% for item in cart.items %}
    {% assign joinedTags = item.product.tags | join: " " %}                
    {% assign cartTags = cartTags | append:" " | append:joinedTags %}
{% endfor %}

{% if cartTags contains 'string1' and cartTags contains 'string2' %}
    {{cartTags}}
{% endif %}

What I have done here is, initialized an empty variable named cartTags. Then for each product in the cart, I convert the tags array to string using join filter and add them to all tags using append filter.

Product Tags

Join Filter

Append Filter