Shopify - How To Show/hide Cart {{ Note }} Depending On If It Is Left Empty Or Not In Email Notification

- 1 answer

I have a {{ cart.note }} section added on my Cart pages, which I'm using for in-house Purchase Orders. For customer email notifications I'd like this field to display instead of the default Invoice number, but only if that field is filled out (if left empty then the default invoice # will display).

I've tried writing this in the email notification section:

{% if note != '' %}Purchase Order #: {{ note }} 
{% else %}Order {{ order_name }} 
{% endif %}

I have also try replacing note in the if statement with cart.note and it did not change anything.

Though it ends up showing the first line even when it is left empty (never displaying the 2nd line as well).

In case it helps, this is from the cart-template.liquid

<textarea name="note" class="input-full" id="CartSpecialInstructions">{{ cart.note }}</textarea>


A: In Liquid, '' (empty string) is not the same as null - use the blank keyword to cover all cases

In Liquid, some comparisons and truthy/falsey values do not behave as one might expect when coming from a different language. Fortunately, the blank keyword exists, which will match all of the following:

  • null/nil
  • false
  • '' (empty string)
  • []/{} (empty array/object)

Changing your comparison to {% if note != blank %} should get your code behaving exactly as you expect.


PS: Alternatively, if you prefer reading positive comparisons (or just want to be fancy), you could use the equivalent 'unless' statement:{% unless note == blank %} ... {% else %} ... {% endunless %}