skip to Main Content

I’m using Shopify and want to hook into customer tags, however they are case sensitive. So {% if customer.tags contains "wholesale" %} is not the same as {% if customer.tags contains "Wholesale" %}. My client may or may not stick to one case when applying tags so I want to guard against that in the future.

I would like to take an array, customer.tags, and convert all of the values to lowercase. I’m trying to work out the logic but am having trouble.

I want to put customer.tags into a new array which isn’t working.

{% assign newArray = customer.tags %}
{{ newArray }}

What am I doing wrong?

3

Answers


  1. You could use the downcase filter for this:

    {% assign contains_wholesale = false %} 
    
    {% for tag in customer.tags %}
      {% assign lowercase_tag = tag | downcase %}
        {% if lowercase_tag == 'wholesale' %}
          {% assign contains_wholesale = true %}
        {% endif %}
    {% endfor %}
    

    Note: downcase just works for ASCII characters. If you need to search for strings with accented letters or other Unicode characters then this won’t be enough.

    Login or Signup to reply.
  2. Another solution as you use the “contains” operator would be to skip the “w”.

    Something like {% if customer.tags contains ‘holesale’ %} should work.

    Login or Signup to reply.
  3. if you would like to keep customer.tags as an array so you can keep using contains in a simple if statement (like your example). You could also chain a couple of liquid filters together to turn all of the strings within the array to lowercase.

    Example:

      {% assign lowercaseTags = customer.tags | join: ',' | downcase | split: ',' %}
      {% assign randomString = 'WholeSale' | downcase %}
    
      {% if lowerCaseTags contains randomString %}
        {% comment %}
        Will now match regardless of case sensitivity
        {% endcomment %}
      {% endif %
    

    Explanation:

    • Join filter: Turn the array into a string seperated by ,
    • Downcase filter: make the whole string lowercase
    • Split filter: opposite of join, recreates the array from the string based on the character used in join ,
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search