# How to use custom variables in price formulas

Custom price calculators can get easily out of hand in terms of complexity and readability. Custom variables allow you to define a formula, assign it a name, and reuse it anywhere in a price formula. This helps break down a complex formula into smaller, more manageable, and more readable parts.

For example, imagine that we have the following formula: `( [Width] * [Length] / 10000 ) * [Thickness.value]`

This contains 2 parts:Â `[Width] * [Length] / 10000`

is the area in square meters, and `[Thickness.value]`

is the value associated with the choice of thickness being selected by the customer.

You can simplify this by creating a custom variable called 'Area' which consists of `[Width] * [Length] / 10000`

. You can then use `[Area]`

in the main formula, instead of the original expression.

## Setup instructions

- Go toÂ
*Products â†’ Options*, edit the options group containing the price formula, and open up the price formula option. - Click the "+" button under the 'Custom variables' section.
- Type a name for the variable. (Tip: Remember that variables should have unique names across the whole option group, and avoid the reserved variable names.)
- Click in the 'Formula' field for the custom variable to activate it, and then click on the buttons underneath the main price formula field to add these variables. Alternatively, you can enter the formula for the custom variable manually by copying the relevant parts from your main price formula.
- Finally, use the 'Custom variables' button which appears underneath the main price formula and select the variable you just created. This will add it to the formula.

## Reserved variable names

When writing a price formula, you should avoid certain variable names which are reserved for other mathematical constants and would create a conflict. The reserved names are:

**PI**: the number Ï€;**E**: the Euler's number;**LN2**: the natural logarithm of 2;**LN10**: the natural logarithm of 10;**LOG2E**: the base-2 logarithm of E;**LOG10E**: the base-10 logarithm of E;**SQRT1_2**: the square root of 0.5**SQRT2**: the square root of 2

Custom variables can be thought of as independent formulas that are assigned a name so that they can be used inside the main formula.

## Strategies around custom variables

### Use them!

It is often useful to define as many custom variables as possible - particularly in complex price formulas. You can also use intermediate variables if needed (i.e. referring to one custom variable from another custom variable).

The screenshot above uses custom variables recursively inside the subsequent variables.

If you do this then it's important to be careful with cycle references, as two custom variables that mutually reference each other can lead to unpredictable results. For this reason, you should use a top-to-bottom approach, where you start defining your variables from the top and move down as needed.

### Use the same custom variable multiple times

Custom variables are particularly useful when a certain expression is used multiple times inside a formula. Not only does this reduce the length of a formula but it also makes it much easier to change in future, preventing avoidable mistakes.

Let's consider our original formula `( [Width] * [Length] / 10000 ) * [Thickness.value]`

and imagine that we want to calculate the result differently depending on the value of `[Width] * [Length] / 10000`

. For example, if the area is more than 2 square meters then we want to add a 20% overcharge. Without using custom variables, the formula would be:

`if ( gt( [Width] * [Length] / 10000, 2 ), 1.2, 1 ) * ( [Width] * [Length] / 10000 ) * [Thickness.value]`

When using an `[Area]`

custom variable, we can simplify that formula as follows:

`if ( gt( [Area], 2 ), 1.2, 1 ) * [Area] * [Thickness.value]`

In this formula, we used two logical functions: `if()`

and `GT()`

. If you want to learn more, please read the article about logical functions.

### Use them to store constant values

The formula field of a custom variable is designed to contain a mathematical expression. A number is also a mathematical expression. This means that you can use custom variables to write constants instead of scattering them inside the main formula. This strategy allows you to write a constant only once so that you can change it at a later time, if needed, without being forced to update the entire price formula.

Let's see the following example:

`[product_price] + [Width] * [Length] * 25`

where `25`

might be the cost of material by the measure.

If we replace `25`

with a custom variable, then we could have a configuration like the one below:

On the surface, it may seem that we complicated the formula compared to the previous configuration. However, let's imagine a scenario where `25`

was used several times in the formula and the shop owner needs to periodically change that number. By using a custom variable, we isolated the place where that number is written to a single location, which makes it much easier and much less error-prone to change it at a later time.

### Alternative formulas

Custom variables are also useful if you need to periodically change elements of the formula. For example, imagine that you switch between Regular and Special pricing depending on whether you're running a sale.Â Instead of updating the pricing in your main formula for each sale, you can create 2 custom variables (Regular and Special) containing the relevant pricing; plus another custom variable which allows you to indicate which type of pricing to use:

The screenshot above uses logical functions to create two alternative calculations through the use of the `[Use_Regular]`

custom variable. This is what happens: the custom variable `[Regular_Price]`

is calculated as the multiplication of `[product_price]`

by `[Width]`

and `[Length]`

converted to square meters; the custom variable`[Special_Price]`

is defined as 80% of `[Regular_Price]`

. (i.e. with a 20% discount);Â ultimately, `[Final_Price]`

returns `[Regular_Price]`

when `[Use_Regular]`

is `1`

and returns `[Special_Price]`

when `[Use_Regular]`

is 0.

The shop owner doesn't need to update the content of the formulas when it's time to start or end a sale. They simply need to switch `[Use_Regular]`

to either `1`

or `0`

depending on which custom variable they want to activate.