Check that several of certain item condition is in the cart?

I have some special fees that get added along with every order (i’m using both an app and some other custom code). I’ve seen some examples where they dont work and dont get added, but I cant replicate it. All the fees have an “" on them like "ConvenienceFee”, so I have a simple protection built in which checks that at least one of the items in the cart contains an aseterik and it throws up an error message at checkout. I’ve realize this isnt sufficient. I have examples where one fee gets added and others do not. So I’d like to do this same check but check that it finds at least 3 items in the cart with an asterik.

So when “isFeeInCart” is true - it means no fee is in cart and I display an error message. So I need that to become false only after it finds the “*” three times.

Please help me with modification to this code to do this? Thank you.

{% assign isFeeInCart = true %}
{% for item in cart.items %}
    {% if  item.product.title contains "*" %}   
      {% assign isFeeInCart = false %}
   {% endif %}
  
  {% endfor %}

Something like this might work. You create a new variable that keeps track of the number of asterisks.

{% assign isFeeInCart = true %}
{% assign asterisk_count = 0 %}
{% for item in cart.items %}
    {% if  item.product.title contains "*" %}   
      {% increment asterisk_count %}
      {% if  asterisk_count >= 3 %}
         {% assign isFeeInCart = false %}
      {% endif %}
   {% endif %}
  
  {% endfor %}

Hi @JoeCat
Please try this

{% assign isFeeInCart = false %}
{% for item in cart.items %}
    {% if  item.product.title contains "***" %}   
      {% assign isFeeInCart = true %}
   {% endif %}
  
  {% endfor %}

Thanks!

Thanks but no it’s not this, I’m looking for multiple items that each have one * each

This is great. This should work, I’ll test it shortly. Thanks

Alright, let me know if it doesn’t work and I’ll try to come up with something else.

This was a great solution. I tested and it didnt work, it seemed the increment function didnt work - I used an alternative to the incrementing (I changed the variable name for my own reference) and it works perfect. Thanks so much for your help.

{% assign isFeeInCart = true %}
{% assign fee_count = 0 %}
{% for item in cart.items %}
    {% if  item.product.title contains "*" %}   
 	{% assign fee_count = fee_count | plus: 1 %}
      {% if  fee_count >= 2 %}
         {% assign isFeeInCart = false %}
      {% endif %}
   {% endif %}
  
  {% endfor %}

That’s odd, the increment function is built into Liquid. But I’m glad that you were able to make a work-around.

Happy to help, you’re welcome!