All Collections
Common use-cases of the Expression language
Common use-cases of the Expression language
Erik Mancír avatar
Written by Erik Mancír
Updated over a week ago

Expression language as a condition in the Automation rules

If you are using expression language as a condition in the Automation rules, then the beginning and ending characters {% %}, which are needed when using the expression language in the editor, should be removed from the condition in the rule.

Comparison operators

== (Equal)
Let's say you need to automatically trigger the order where at least one of the item's name is "Jeans".
The best way to do it is to filter out the line_items where the name == "Jeans" and wrap it up into the aggregate function Count. Then you can make a condition to see if the count of those items is greater than 0.

count(filter({line_items}, 'name == "Jeans"')) > 0 

View from the automation rules:

N.B. In expression language, the sub-items are accessed with brackets.
For example, you need to access the value of a location name in your line items meta fields and set the condition to trigger the rule if there is at least one item with the value of location name "abc".

The expression used in the automation rules would be:

count(filter({line_items}, 'line_item_metafields["location_name"]["value"] == "abc"')) > 0

Matches (Regex match)
As a condition, you can also use some other operator, for example, Matches (Regex match). This way you can trigger the rule if the data field consists of some part of the string.

{product_name} matches "/Jeans/"

With the expression above, all kinds of product names that consist of a substring "Jeans" would pass the condition.

You can find all the other operators and functions when you navigate to the template editor, then click "Help" on the top of the editor and choose "Expression functions".

Automation rule with the Shipping method condition
If you want to make a condition based on the shipping method or any other array object, keep in mind that you need to use the expression to directly access the array data.
The expression would look like this:

{shipping_lines}[0]["title"] == 'abc'

Changing the timezone of a date

Dates are usually pulled to the document editor with the original timezone from the e-commerce platform and you need to use the DateTime expression function with timezone to convert the times in the editor.
{% datetime({dateValue}, 'UTC') %}
The expression function needs timezone in PHP format, which you can find here:

For example:

{% datetime({TestDate}, 'Europe/London') %} 

Function Str_replace

The str_replace([string: search], [string: replace], [string: the original string]) function replaces all occurrences of a substring within a string.

{% str_replace("dog", "cat", "this is dog") %}
This expression output is: this is cat

Useful tip:
If you have a long string of, for example, options text, where each option is separated with "/" and you would like to show each option on a separate line, you can use the function str_replace.


Condtional Formatting
You can also use the expression language in the conditional formatting, where you can change the style or hide the component if matches the condition. To open the Conditional formatting view, you need to right-click on the component and select "Conditional formatting".

Did this answer your question?