Display result if certain criteria met is ANY of cart line items

Solved
New Member
2 0 0

I'm trying to prevent a certain widget from displaying in the cart page if a certain product IS in the cart.

 

I have achieved this if the product in question is the only item in the cart, or even if it is the first item in the cart. But if there is anything above it, it displays the widget.

 

I understand why my code below won't work to achieve the result I'm looking for (it's recursive, so it's looking at each line item individually and because it doesn't see the 'xxxx' in a line item, it displays the widget and moves on), but I'm not yet grasping how to actually make it work as I need it to.

 

{% for item in cart.items %}

       {% if item.product.title contains 'xxxx' %}

               <!— Display Nothing —>

       {% else %}

              Display This

       {% endif %}

{% endfor %}

0 Likes

Success.

Astronaut
1082 147 272

Loop through your line items first and assign a variable condition to be true if your conditions are met, then you can loop through them again after and you can include your widget based off the boolean:

{% assign item_in_cart = false %}
{% for item in cart.items %}
       {% if item.product.title contains 'xxxx' %}
          {% assign item_in_cart = true %}
       {% endif %}
{% endfor %}

{% unless item_in_cart %}
//include your widget
{% else %}

{% for item in cart.items %}
//normal code
{% endfor %}

 

If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
1 Like
New Member
2 0 0

That got me where I need to go, thank you!

0 Likes