creative designer

If you must check if something is a number within liquid there’s neat little trick you can use with the existing json filter. The filter has it’s own datatype check in place so it knows whether to wrap the value in quotes of not. It’s the existence of those quotes that we can use for our test. Case in point:

{% assign var1 = "1" %}
{% assign var2 = 1 %}
{% capture test_var1 %}{{ var1 | json }}{% endcapture %}
{% capture test_var2 %}{{ var2 | json }}{% endcapture %}
var1 = {% if test_var1 contains '"' %}string{% else %}number{% endif %}
var2 = {% if test_var2 contains '"' %}string{% else %}number{% endif %}

That should output:
var1 = string
var2 = number

Of course there’s other methods too so check out the related thread on the ecommerce forums. A few other Shopify Experts have dropped some ideas as well.

Like to work with me?

Let's talk