if customer.tags contains doesn't seem to search for part of a tag...

KenMc
Excursionist
13 1 1

Any clue why this doesn't seem to work? I thought the contains operator checks the substring inside a string but it doesn't seem to work with the customer.tags unless you have the full tag as the substring.

Example customer tags like ProDeal, ProStuff, ect. 

{% if customer.tags contains "Pro" %}Hello Pro {% endif %}

 

 

0 Likes
camb
Tourist
3 0 1

Hey @KenMc , 

I'm not sure if you're still facing this issue, but I figured I'd answer for any future troubleshooters.

The reason you're not able to find substrings of tags is because of the way the contains operator works.

As per Shopify's Liquid documentation , there are 2 ways to use contains: 

  • looking for an exact substring within a string
  • looking for an exact string within an array of strings

So basically, you're trying to use it both ways at once, but it can only do either/or. 

You can loop over your tags and use the string version of the operator like so:

{% assign is_pro = false %}
{% for tag in customer.tags %}
    {% if tag contains "Pro" %}
       {% assign is_pro = true %}
       {% break %}
    {% endif %}
{% endfor %}

{% if is_pro %}Hello Pro {% endif %}

----
Personally, I don't like all of the {% %} brackets, so I like to put all of my logic at the top of the file and use the new {% liquid %} tag like so:

{% liquid
assign is_pro = false
for tag in customer.tags
    if tag contains "Pro"
        assign is_pro = true
        break
    endif
endfor
%}
 
Then, in your markup, you can use:
{% if is_pro %}Hello Pro {% endif %}



----


Hopefully this helped you (or someone else) if you were still having this issue.

Best,
Cam

0 Likes