# Calculation fields

Calculation fields allow you to make calculations using formulas. The formulas can include values inputted by the user through other fields, as well as fixed values, the product price, product properties like dimensions and weight, and other variables that you can set.

Calculation fields are available with the Pro version of the licence.

## Creating a calculation field

Choose ‘Calculation’ as the field type. You can give the field a label if you wish.

In the ‘Formula’ field, enter the formula that will be used to make the calculation. You can use the following tags:

### Field tags, e.g. `{field_101}`

In the image above, you can see that the formula contains the tag `{field_960}`

. This means that the calculation will use the value from the field with the ID 960.

Note that when using field tags:

- Fields must be in the same product
- Fields can be either number fields or other calculation fields

### Product price tags, `{product_price}`

You can include the product price in your formula, e.g.:

{field_960} + {product_price}

This will return the sum of the value of field ID 960 and the price of the product.

### Global variables, e.g. `{variable_1}`

There is scope to set three global variables that can be used in your calculation fields. These are represented as `{variable_1}`

, `{variable_2}`

and `{variable_3}`

.

You can set values for global variables from WooCommerce > Settings > Product Add-Ons.

#### Extending global variables

If the three global variables available to you aren’t enough, you can create as many unique variables as you like.

Set name / value pairs as in the snippet below:

<?php | |

/** | |

* Create multiple variables for calculation fields | |

*/ | |

function prefix_calculation_global_calculation_vars( $vars ) { | |

return array( | |

'my_first_variable' => 50, | |

'price_modifier_1' => 150 | |

); | |

} | |

add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' ); |

You can follow the steps here to add a snippet to your site.

### Field price, e.g. `{field_999_field_price}`

To get the price value of a field, use:

{field_999_field_price}

### Option price tags, e.g. `{field_999_option_price}`

You can get the value of selected options for radio and select fields, eg:

{field_999_option_price}

This will give you the value of the selected option for field 999.

### Number of uploads

You can get the number of uploaded files by field, e.g:

{field_999_number_uploads}

This will return the number of files uploaded to field ID #999.

### Look up tables

Look up tables allow you to pull prices from a pricing grid, based on user inputs. There is a dedicated article here on how to use them.

### Dimensions and weight

Use the following tags in your formula for dimensions and weight:

{product_weight} {product_length} {product_width} {product_height}

### Operators

You can use the following operators: +, -, *, /

You can use parentheses to determine the order of operations within the calculation.

For example:

( ( {field_950} * {field_951} ) + {product_price} ) / {variable_1}

You will need to ensure that your parentheses balance correctly otherwise the formula won’t evaluate.

### Rounding

You can choose to round up or round down to the nearest whole number. Just select the desired option from the ‘Round Result’ field.

### Decimal Places

Set how many decimal places you’d like to return your result to.

### Action

The ‘Action’ setting allows you to define how the calculation result might affect your product page.

- Choose ‘Display As Cost’ if the calculation result will affect the cost of the product
- Choose ‘Update Quantity’ if the calculation result will change the quantity selected

## More information

There are several example products using calculation fields in this article.