View Categories

Conditional Email Content with [if]

Additional Custom Emails & Recipients for WooCommerce ships an [if] shortcode that shows or hides a block of email content based on a comparison. Use it to add a line only for large orders, greet customers who paid with a specific gateway, or drop a note when a piece of order metadata matches a value, all inside a single email instead of maintaining several.

[if] is a general shortcode: it works in the Subject, Email heading, and Email content fields of any custom email, configured under WooCommerce > Settings > Emails.

The syntax #

[if value1="A" operator="equal" value2="B"]Shown only when A equals B[/if]

All three attributes are required, and the enclosed content must not be empty; if any is missing, the shortcode outputs nothing. When the comparison is true, the enclosed content is rendered (and its own shortcodes are processed); when false, it produces an empty string.

Operators #

Operator True when
equal value1 equals value2
not_equal value1 does not equal value2
less value1 is less than value2
less_or_equal value1 is less than or equal to value2
greater value1 is greater than value2
greater_or_equal value1 is greater than or equal to value2
in value1 is one item in the comma-separated list value2
not_in value1 is not in the comma-separated list value2
find value1 appears somewhere inside value2
not_find value1 does not appear inside value2

The less, less_or_equal, greater, and greater_or_equal comparisons follow PHP’s loose comparison rules, so two numeric strings are compared as numbers. That is what you want for order totals and quantities.

Using shortcodes inside the comparison #

You will usually want to compare a live value from the order, not a fixed string. Because value1 and value2 are attribute values, you cannot put square-bracket shortcodes directly inside them (the nested brackets would break the outer shortcode). Instead, write the inner shortcode with curly braces, and the plugin converts { } to [ ] and runs it before comparing:

[if value1="{order_total}" operator="greater" value2="100"]
Thanks for your large order! Here is a gift for you.
[/if]

Here {order_total} is evaluated to the order’s total, then compared against 100. Any shortcode from the plugin’s set can be used this way, for example {order_payment_method_id} or {order_meta key="_some_key"}.

Case-insensitive matching #

Add case_insensitive="true" to compare text without regard to letter case. Both sides are lowercased before the operator runs:

[if value1="{order_payment_method_title}" operator="find" value2="paypal" case_insensitive="true"]
You paid with PayPal.
[/if]

More examples #

Show a block only for specific payment gateways:

[if value1="{order_payment_method_id}" operator="in" value2="bacs,cheque"]
Please complete your bank transfer or send your cheque to finish the order.
[/if]

Add a note when a metadata flag is set:

[if value1="{order_meta key='_is_gift'}" operator="equal" value2="yes"]
Your order will be gift-wrapped.
[/if]

Note the single quotes around the inner attribute value: use single quotes inside a curly-brace shortcode so they do not clash with the double quotes of the outer value1 attribute.

Notes and limits #

  • All of value1, operator, and value2 must be present, and the content between the tags must not be empty, or the shortcode renders nothing.
  • The rendered content is filtered with WordPress’s post-safe filter, so standard HTML survives but disallowed markup is stripped.
  • [if] can be nested: because the true branch is run through the shortcode processor again, you can place another [if] inside it.
  • When an order value is empty (for example an order shortcode used in a non-order email), the comparison still runs against the empty string. Account for that when using equal or find.

Build smarter emails #

Conditional content keeps your template count down. Combine [if] with the full shortcode set in Additional Custom Emails & Recipients for WooCommerce to tailor a single email to every order. Get the plugin to start building.