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

### Comparison functions

#### compare

`compare( a, b, operator )`

The `compare()`

function accepts exactly 3 arguments: two operands and one operator. `operator`

can be one of the following strings: `>`

, `>=`

, `=`

, `!=`

, `<=`

, and `<`

. If `operator`

is not provided or is a string different from the accepted operators, then `>`

will be used. The `compare`

`()`

function evaluates whether the expression `a operator b`

is true or not. If it is `true`

, the function returns `1`

. If not, it returns `0`

. For example, when `a = 10`

, `b = 30`

and `operator = "<="`

, the function will evaluate whether `10 <= 30`

and return `1`

.

#### eq

`eq( a, b )`

The `eq()`

function (**eq**ual) accepts exactly two arguments and returns `1`

if `a`

is equal to `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, "=" )`

.

#### neq

`neq( a, b )`

The `NEQ()`

function (**N**ot** EQ**ual) accepts exactly two arguments and returns `1`

if `a`

is not equal to `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, "!=" )`

.

#### gt

`gt( a, b )`

The `gt()`

function (**g**reater **t**han) accepts exactly two arguments and returns `1`

if `a`

is greater than `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, ">" )`

.

#### gte

`gte( a, b )`

The `gte()`

function (**g**reater **t**han or **e**qual to) accepts exactly two arguments and returns `1`

if `a`

is greater than or equal to `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, ">=" )`

.

#### lt

`lt( a, b )`

The `lt()`

function (**l**essÂ **t**han) accepts exactly two arguments and returns `1`

if `a`

is less than `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, "<" )`

.

#### lte

`lte( a, b )`

The `lte()`

function (**l**ess **t**han or **e**qual to) accepts exactly two arguments and returns `1`

if `a`

is less than or equal to `b`

. It returns `0`

otherwise. This function is a shorthand of `compare( a, b, "<=" )`

.

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