Logical Operators (And, Or, Not) in Order Rules

This article explains the use of logical operators in PackageBee order rules.

Overview

PackageBee 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 PackageBee:

  • 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.

Example

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).

  1. Open the desired store.
  2. Go to the Rules tab.

PackageBee - Rules

  1. Click +Add rule.

PackageBee - Add a rule

  1. Give your rule a descriptive name. Then click +Add action.

PackageBee - Name the rule

  1. Select an action to perform from the dropdown, e.g. Condition.

PackageBee - Select action to perform

  1. 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.

PackageBee - Select a logical operator.png

  1. 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.

PackageBee - Compare order attribute

  1. Click the blue +Add action button in the active rule window.

PackageBee - Add action

  1. Select the action to perform from the dropdown menu. For our example, we will select Shipping Transformer.

PackageBee - Shipping transformer

  1. From the dropdown menu, select the Ship via value that meets your needs. For our example, we will select FedEx International Priority.

PackageBee - Ship via

  1. To finish, click OK, then click Save changes.

PackageBee - 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
  • Associativity:
    • 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
  • Distributivity
    • 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

For example:

  • 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.

Source code

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"
when
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")))
then
reject order
end
end

This rule accepts only orders that are to be shipped to either Canada or the US, except the states of Alaska and Hawaii.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.