Formulas and variables explained

Advanced Product Fields for WooCommerce (APF) allows you to create complex product price calculators through formulas. Formulas calculate a custom product price based on the data your customers select or 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 (length and width) and the shelves can be cut in any format. The final product price will depend on the length and width supplied by the customer.

The product page looks like this:

A custom made-to-measure product example in WooCommerce

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

  • Length (in cm)
  • Width (in cm)

Based on what the customer entered, the final product price will be calculated.

This requires a simple formula to multiply the length and width with a cost per centimeter ( = the unit cost): 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 length and width 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, enable the field’s Adjust Pricing setting. Set the pricing type to Formula-based pricing. In the input field next to it, you can type your formula.

Formula based pricing in APF field settings

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:

Multiple choice options with formula based pricing

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 how you can build your formula!

Formula syntax

Before you can write formulas, you should understand the syntax ( = the way to write formulas). Below are all the items necessary to learn how to write formulas!

Basic arithmetic equations

Just like any formula builder, you can write basic arithmetic operations with these math symbols: + - * /. You can also use brackets to group operations that should be calculated first. Here are a few simple examples of valid formulas:

  • 10 * 2
  • (0.15 + 20) - 1
  • 100 / 1.5

Dynamic codes

Aside from numbers and basic arithmetic operations, you can also use dynamic codes. These codes dynamically fill in the number they represent. Here is a list of all the possibilities:

  • [price] to get the base product price. That’s the price you set up for this product in the General settings within WooCommerce.
  • [qty] to get the chosen quantity (via the input field next to the “add to cart” button).
  • [field.{id}] to get another field value. Replace {id} with your field’s ID as found in the backend. Here’s an example: [field.5e8c417]
  • [price.{id}] to get the price of another field. Replace {id} with your field’s ID as found in the backend. Here’s an example: [price.5e8c417]
  • [options_total] to get the current options total. Please note this is only available in our Extended version.
  • [var_yourvarname] points to a variable. The concept of variables is explained further down in this guide.

Here are a few example formulas using dynamic codes:

  • 5 * [qty] adds a cost of $5, multiplied by the chosen quantity.
  • [field.5e8c417] + 10 adds a cost of $10 plus the value of field with ID 5e8c417.

Functions

Our formula system works similar to that of Google Sheets or Excel. You can use functions in your formulas (such as if(), round(), etc..) to make your pricing even more powerful!

Functions in formulas are predefined operations that perform specific calculations, like round(1.65) to round a decimal number, or files() to count the number of uploaded files. In short, they simplify complex tasks.

Find the full list of available functions here.

Formula checklist

Writing formulas can be tricky, and small mistakes can cause unexpected results. If your formula isn’t working as expected, check the following:

Creating a size formula to multiply length and width by a unit price

Now that we know how formulas work, let’s circle back to our example product from earlier and create a formula to multiply the length and width by a unit price.

The base product price is $55. The additional price depends on the length and width 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: length and width. Only the “width” field should have the “pricing” option enabled. The formula used to calculate the price is:

([field.c90b10e]*[field.33c54cc])*0.005

To understand this formula, let’s break it down in parts:

  • [field.c90b10e] points to the value of the “Length” field. Remember the [field.{id}] shortcode we explained a little earlier in this article.
  • Similarly, [field.33c54cc] points to the value of the “Width” field.
  • 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.

Multiplying by quantity

Since customers can increase the quantity of a product in their cart, you need to make sure the final price reflects the selected quantity. To do so, you must multiply your calculations by the product quantity using the [qty] shortcode. Your  formulas should look like this: 

(...insert your formula here...) * [qty]

Here’s a screenshot for reference:

multiply your formula with [qty]

Find a field ID

As you’ve probably already guessed, many formulas will point to values of other fields to do calculations. To do that, you can use the [field.{id}] shortcode (as explained above). But how do you find the field ID that should replace {id}? Easy, each field header contains the ID:

Find field IDs in APF

Clicking the ID will copy it to clipboard so you can easily use it in formulas.

Use the formula helper

An easier way to do things is to use the formula helper. It can be accessed by clicking the little question mark:

Click the question mark to access the formula builder helper

This tool helps you navigate all available functions, fields, and you don’t have to think about field ID’s:

Using the formula helper in APF

You can click on “function glossary” to learn about each function available and see some examples.

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:

pizza-builder-toppings

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. Scroll down until you find the “variables” section:

And click Add new Variable:

Exaample variable in APF

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:

Configuration of pizza toppings with variable and formula pricing

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.

Was this article helpful?

Related Articles