# Formulas and variables explained

Advanced Product Fields for WooCommerce allows you to create complex product price calculators through formulas. Formulas calculate a custom price for the product based on the data which your customers enter in the product options.

A formula is a mathematical equation that results in a number. A basic example of a formula is `1+2` which results in `3`.

## An example of how it works

Let’s say you’re selling made-to-measure wooden shelves. Customers can enter their desired size (width and height) as the shelves can be cut in any format. The final product prize will depend on what the user entered in the product options. The product page looks like this:

As you can see, the product page shows 2 options created with Advanced Product Fields for WooCommerce:

• Width (in cm)
• Height (in cm)

Based on the user’s data, the final product price will be calculated. This requires a simple formula to multiply the width and height with a cost per centimeter: `width * height * unit cost`. Let’s take a look at how to set this up and how to create the formula!

## Adding formula pricing to a field

For this tutorial, we assume you already know how to add the width and height options to your WooCommerce product. If you need help with that, we recommend reading the tutorial on how to get started with Advanced Product Fields for WooCommerce.

To add a formula, find your field’s Adjust Pricing setting and enable it. Set the pricing type to Formula-based pricing. In the input field next to it, you can type your formula.

If you want to add a formula to a field with multiple choices (such as a select list, checkboxes, or any of the swatches), you will find the same settings in the Options section of the field:

Once you enabled pricing and set the pricing type to Formula-based, you can enter a formula in the field next to it.

Now let’s take a look at what codes you can use inside a formula.

## Dynamic codes & functions

Aside from numbers and elementary arithmetic operations (+, -, * and /) that make up a basic formula, you can also use dynamic codes. These codes dynamically fill in the number they represent. See a list of all possibilities:

• `[price]` to get the base product price.
• `[qty]` to get the current product quantity.
• `[x]` to get the current field value.
• `[options_total]` to get the current options total. Please note this is only available in our Extended version.
• `[field.{id}]` to get another field value. Replace `{id}` with your field’s ID as found in the backend. Here’s an example: `[field.5e8c41711d1db]`
• `[var_yourvarname]` points to a variable. The concept of variables is explained further down in this guide.

You can also use functions in your formulas (such as `if()`, `round()` etc..) to make your pricing even more powerful. Find the full list of available functions here.

### Creating a size formula to multiply width and height with a unit price

Now that we know what codes we can use inside a formula, let’s circle back to our example product from earlier and create a formula to multiply a custom width and height with a unit price.

The base product costs €55. The additional price depends on the width and height filled out by the customer. The formula calculates the total amount based on a unit price. 1 square cm costs 0.5 cents. The fields in the backend look like this:

We’ve created two number fields: width and height. Only the “height” field should have the “pricing” option enabled. The formula used to calculate the price is:

`([field.5e8ecdf46ea7d]*[x])*0.005`

A few things are notable here:

• `[field.5e8ecdf46ea7d]` points to the value of the “width” field.
• `[x]` points to the value of the “height” field itself.
• We multiply this with the unit price for a square centimeter: `0.005` .
• We used parenthesis (brackets) to group things together. Learn more about why brackets are important in math.

## Troubleshooting formulas

Are you playing around with formulas but noticing they don’t work? We have a troubleshooting article with some tips on how to avoid faulty formulas.

# Variables

Advanced Product fields allows you to create variables which can then be used inside formulas. The variable can be static (meaning it only has one value) or dynamic. A dynamic variable can change value depending on other values of product options. Below, we explain this concept with an example.

## Example of using variables in a formula

Let’s say you’re running an online pizza shop. Buyers can choose toppings to put on their pizza at a default price of \$1 per topping. Here’s how this might look on your website:

Buyers can also choose the size of their pizza. When selecting “Large”, the pizza would be double the size of a small one. So naturally, it would contain more toppings. Therefore, if a user selects “large”, you want each topping to cost \$1.50 instead of the default price of \$1.

This scenario can be solved by creating a variable:

First, we gave the variable a name. We called it `topping_prize`. Then, we set the default value to `1` because per default each topping costs \$1.

In the “value changes” section, we add rules to make this variable dynamic. In our case, we need only one rule: when the “Large” size is selected, the variable should be 1.50 instead of 1.

Now, we can use this variable in the price settings of our toppings:

The formula is `[var_topping_prize]*[qty]`. Here’s what you should know:

• `[var_topping_prize]` is the name of the variable we created earlier. Note that each variable starts with `var_`.
• We multiply by `[qty]` to make sure that if the user wants two pizzas, the topping prize is also multiplied with the quantity.

If the user selects a large pizza, each topping they add will cost \$1.50. In all other cases, toppings cost just \$1.