1. Home
  2. Knowledge Base
  3. WooCommerce Product Options
  4. Price Formulas

How to use logical functions in price formulas

The price formula field type in WooCommerce Product Options allows store owners to create custom calculations for product prices based on information that the customer provides. When writing a price formula, it is often useful to have logical functions that dictate how to calculate the formula based on the available variables. In this article, you will learn everything you need to know about the logical functions our plugin comes with and much more.

Writing conditions as functions

Those who are used to programming languages certainly understand a conditional clause such as:

if ( x > 10 ) then y = 1 else y = 0

That type of clause would be difficult to write in a formula that must be evaluated as a mathematical expression. For this reason, we embraced the same approach that has been used for decades in Spreadsheets such as Microsoft Excel or Google Sheets. Therefore, if you are already used to that style, you will be immediately comfortable with writing complex conditional expressions in our price formula field.

True is 1, False is 0

One of the basic principles of logical functions, particularly when they are used in a mathematical context, is the equivalence between true and 1 and between false and 0. Let's consider the following expression:

COMPARE( [product_weight], 10, ">" )

This logical function compares two variables using the > operator: if [product_weight] is greater than 10, the function returns true; if it is less than or equal to 10, the function returns false. In a mathematical context, we always assume that true is 1 and false is 0. This way, we can take advantage of that equivalence in our calculations. For example, let's consider the following formula:

[product_price] * ( 1 + 0.25 * COMPARE( [product_weight], 10, ">" ) )

and let's start evaluating it with a practical example. Our product costs $100 and weighs 12 kg. So, we can replace those values in the formula, which becomes:

100 * ( 1 + 0.25 * COMPARE( 12, 10, ">" ) )

Now, 12 is greater than 10, so the COMPARE() function returns 1, which gets us to the following result:

100 * ( 1 + 0.25 * 1 ) = 100 * ( 1 + 0.25 ) = 100 * 1.25 = 125

In plain English, what the formula means is: "If the weight of the product is greater than 10 kg, then increase the price by 25%".

Logical Operators

WooCommerce Product Options also includes logical operators that you can use directly inside a formula. Those operators are <, >, <=, >=, = and !=. The use of those operators allows writing conditional formulas such as the following one:

[product_price] * ( 1 + 0.25 * ( [product_weight] > 10 ) )

with the same meaning as the previous version of the formula. It is worth noting that a logical expression such as [some_variable] > 100 equals to 1 if some_variable is greater than 100 and 0 otherwise.

Logical Functions

The logical functions that you can use in price formulas are listed below.

Conditional functions

if

if( condition, value_if_true, value_if_false )

The if() function accepts exactly three arguments. If condition is evaluated as 1, then the function returns value_if_true. Otherwise, it returns value_if_false. This function works the same way as in Microsoft Excel or Google Sheets.

and

and( condition1, [ condition2, ..., conditionN ] )

The and() function accepts one or more arguments and returns 1 if ALL the conditions are true, 0 otherwise.

or

or( condition1, [ condition2, ..., conditionN ] )

The or() function accepts one or more arguments and returns 1 if AT LEAST ONE of the conditions are true, 0 otherwise.

not

not( condition )

The not() function accepts exactly one argument and returns 1 if condition is false or 0. It returns 0, if condition is true or 1. Essentially, this function negates its argument, inverting its value.

User-defined functions

For more advanced uses, you can create your custom functions, which can then be used in price formulas like any other mathematical function. To learn more about user-defined functions, you can read the article How to define new custom functions.

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.