Working with conditional formula in Wix Calculator Builder
The IF function is one of the most popular calculator functions. Testing whether conditions are true or false and making logical comparisons between expressions are common to many tasks.
This tutorial explains:
- how to understand the syntax of conditional formulas and write them manually,
- and how to check a few conditions at a time (for example, applying pricing tiers on a final price).
Understanding the syntax of a conditional formula and writing it manually in Wix Calculator Builder
The syntax of a conditional formula is the following:
((IF)?(THEN):(ELSE))
For example:
((#1 - #2 < 500)?(10):(20))
Translating that to English, it would read as follows: IF result of #1 - #2 is less than 500, THEN return 10, or ELSE return 20.
The Logical operators that can be used in a conditional formula:
You can also use the &(and) & |(or) operators to create your conditional formulas. Here’s how it works:
Let’s say you have a calculator consisting of two input fields (ID #1 and #2) and one formula field (ID #3), and you want to check if the values entered to both input fields are not less than 10. And only if the values are not less than 10, only then do you want your formula fields to show, let’s say, 20. And if at least one of the entered values is smaller, then your calculator should show 0. In this case, your formula would look like the following:
((#1 > 10 & #2 > 10)?(20):(0))
Translating that to English, it would read: IF value entered to input field #1 is bigger than 10 and value entered to input field #2 is bigger than 10, THEN return 20, or ELSE return 0.
And if you want to check if at least one condition is met, you can use the OR operator.
The comparison operators that can be used in a conditional formula:
== - Both numbers or inputs are equal.
!= - The value is not equal to the value it is compered to
> - The value is greater than the value it is being compared to.
< - The value is less than the value it is being compared to.
>= - The value is greater or equal to the value it is being compared to.
<= - The value is less than or equal to the value it is being compared to.
A workaround for the ELSEIF function, or checking a few conditions in one formula
Sometimes you might need to check a few conditions for the same input field (when applying differed pricing tiers for different quantities and so on). Although Wix Calculator Builder doesn’t support ELSEIF or IF nesting functions, you can achieve the same result using multiple conditional formulas multiplied with each other.
For example:
((#1 < 10)?(150):(1)) * ((#1 >= 10 & #1 < 20)?(250):(1)) *
((#1 >= 30)?(350):(1))
How this formula reads:
- IF the number entered to input field ID #1 is lower than 10 THEN return 150 or ELSE return 1. Then we multiply this conditional formula by another condition formula that reads:
- IF the number entered to input field ID #1 is greater or equal to 10 and lower than 20 THEN return 250 or ELSE return 1. This formula is also multiplied by another conditional formula:
- IF the number entered to input field ID #1 is greater than 30 THEN return 350 or ELSE return 1.
All these conditional formulas return 1 when the condition is not met, which doesn't influence the final result, and the result is shown for the conditional formulas where the condition is met.