This article explains the use of logical operators in DropStream order rules.
DropStream rule language includes logical operators (also known as boolean operators) AND, OR and NOT that you can use to create more complex rules, and in this way exert greater control over your order fulfillment workflow.
For example, if orders that are being shipped to Canada and require a signature need to be shipped via FedEx International Priority, you can set up a rule that checks that both these conditions hold.
Logical operators have the following meanings in DropStream:
- AND - both conditions need to hold for the rule to apply.
- E.g. use a particular shipping company if the shipping address is Canada AND a signature is required.
- OR - only one of the conditions needs to hold for the rule to apply.
- E.g. reject orders if the shipping destination country OR the billing address country is Great Britain.
- NOT - the rule will apply to anything that is not the value that you set.
- E.g. filter out all order items that do not match certain SKUs.
We demonstrate the use of logical operators by creating a rule for the first example above that contains a conjunction / AND (if the shipping address of an order is Canada AND a signature is required then ship the order via FedEx International Priority).
- Open the desired store.
- Go to the Rules tab.
- Click +Add rule.
- Give your rule a descriptive name. Then click +Add action.
- Select an action to perform from the dropdown, e.g. Condition.
- Select your logical operator from the dropdown next to IF. For this example we will be using AND because we want the order to meet BOTH conditions.
- In the dropdown menus next to your operator, select Compare Order Attribute to determine the conditions that you want to have met. In this case we want the Shipping Address to be Canada AND a signature is required.
- Click the blue +Add action button in the active rule window.
- Select the action to perform from the dropdown menu. For our example, we will select Shipping Transformer.
- From the dropdown menu, select the Ship via value that meets your needs. For our example, we will select FedEx International Priority.
- To finish, first click OK and then click Save changes.
Notes on use
In the example above, both elements of the conjunction were simple conditions. Instead of simple conditions, we could also use conjunctions of conditions, so the total statement will consist of multiple "nested" conjunctions, or conjunctions of conjunctions.
Disjunctions (OR) and negations (NOT), simple or complex, can be constructed analogously.
In complex conditions, the sub-conditions do not need to be of the same type. They can be formed by mixing conjunctions, disjunctions and negations, and by mixing the attributes of orders with the attributes of order items. You can even add quantified conditions that apply only to some items in the order.
The usual properties of the logical operators of Propositional Logic hold, such as
- Double negation: NOT (NOT A) ≡ A
- A AND (B AND C) ≡ (A AND B) AND C ≡ A AND B AND C
- A OR (B OR C) ≡ (A OR B) OR C ≡ A OR B AND C
- Note that conjunction over disjunction and disjunction over conjunction are not associative, so
- A AND (B OR C) ≢ (A AND B) OR C
- A OR (B AND C) ≢ (A OR B) AND C
- NOT (A AND B) ≡ (NOT A OR NOT B)
- A AND (B OR C) ≡ (A AND B) OR (A AND C)
- A OR (B AND C) ≡ (A OR B) AND (A OR C)
- NOT(A is x) is equivalent to (A is_not x) if A is a textual attribute.
- NOT(A = x) is equivalent to (A <> x) if A is a numeric or a date/time attribute.
- NOT(A is blank) is equivalent to (A is not blank) if A is an attribute that is evaluated for any non-empty value.
These constructs use built-in comparison operators that are available from dropdowns.
A sequence of order rules with the same conclusion (THEN part) is equivalent to a conjunction with a shared conclusion, and may sometimes be preferable to keep rules simple. If the rules do not immediately follow each other, the equivalence may not hold because the rules in-between may affect the rules that follow.
In the rule source code used in the Advanced Editor, prefix notation is used for NOT and infix notation is used for AND and OR.
Parentheses can be used for scope disambiguation. If no parentheses are used, negation takes precedence over conjunction and conjunction takes precedence over disjunction:
- NOT A AND B ≡ (NOT A) AND B
- A OR B AND C ≡ A OR (B AND C)
rule "Boolean condition"
not (order.shipping_country is "Canada" or order.shipping_country is "US" and (not (order.shipping_state is "AK" or order.shipping_state is "HI")))
This rule accepts only orders that are to be shipped to either Canada or the US, except the states of Alaska and Hawaii.