Ad

Liquid - Convert Array To Lowercase

- 1 answer

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?

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad