1

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>
jhawes
  • 2,354
  • 3
  • 24
  • 34
  • 1
    Have you tried `{% if note != blank %}`? If the note is `null`, `null` does not equal the empty string - but both are considered equal to the `blank` keyword – Dave B Aug 29 '17 at 15:39
  • @DaveB you nailed it. I had found some past forums that use ' ' to represent blank and that threw me off. I used your recommendation and everything worked great. If you post as an answer I'd like to give you credit. – jhawes Aug 29 '17 at 16:19

1 Answers1

2

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.

Cheers!

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 %}

Dave B
  • 3,038
  • 1
  • 13
  • 18