1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Calculation fields
  1. Home
  2. Knowledge Base
  3. Field Types
  4. Calculation fields
  1. Home
  2. Knowledge Base
  3. Calculations
  4. Calculation fields

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.

There’s a video at the bottom of this page that will give you an overview of the calculation field.

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.

Quantity

You can get the value of the product quantity field to use in your calculation:

{quantity}

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.

Hide Calculation

Select this option to hide the calculation from the page, while still allowing it to function.

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 ‘Set Product Price’ to set the result of the calculation as the total product price
  • Choose ‘Update Quantity’ if the calculation result will change the quantity selected

Advanced Custom Fields

If you are using the Advanced Calculations extension for Add-Ons Ultimate, you can include the value of fields created using ACF in your calculations.

To use ACF fields in your calculations:

  • Create your ACF fields for products. Only ‘Number’ fields will work with calculations
  • Any ACF fields on a product page will be automatically made available for calculations 
  • To use an ACF field in a calculation, you will need to use the ACF field name, e.g. my_number, prefix it with acf_, and wrap it in curly braces – e.g. {acf_my_number} – in your formula

Remember: you’ll need the Advanced Calculations extension to use ACF fields in calculations.

More information

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

You can also check out this video for an overview.

Was this article helpful?

Related Articles