1. Home
  2. Knowledge Base
  3. WooCommerce Product Options
  4. Price Formulas

How to use math functions in price formulas

The price formula field type in WooCommerce Product Options allows store owners to create custom calculations for product prices based on information that the customer provides. Price formulas can be very simple or very complex!

This article provides details on how to use advanced mathematical formulas in your price formulas. The following information is just a starting point and you can use any of the mathematical operators documented in this JavaScript formula parser, which WooCommerce Product Options uses under the covers.

Available variables

You can use price formulas to calculate the final price of a product based on variables such as:

  • Any option in the group. Each option type has different properties that can be used as variables in the formula. You can learn more about those properties in the Using different option types in price formulas section.
  • The properties of the product a group is attached to. The variables you can use are price, quantity being added to the cart, weight, width, length, and height (as defined in the Shipping tab of the product editor).
  • Custom variables defined using any of the previous variables

Mathematical functions and operators

The price formula field type supports common mathematical operators, such as +, -, * and /. In addition, it can parse all the mathematical functions included in the Javascript Math object.

A complete list of those functions can be found on the Javascript Math page of the MDN Web Docs. Those functions can be used by entering their name (without the Math. prefix itself) in the formula. Below, we will explore some practical examples to help you to apply this to different use cases.

How to limit prices

The functions min and max allow you to set a minimum or maximum price that can be charged. For example, you might be selling made-to-measure curtains (like in this demo product) and want to charge a minimum of $50 and a maximum of $500, regardless of the measurements entered.

You should use the max function to set a minimum price and the min function to set a maximum price. (We realise this is the opposite of what you might expect linguistically - this is how these mathematical functions work and is not specific to the WooCommerce Product Options plugin 🙃)

Example: max

Use this to set a minimum price for the price formula.

A store sells made-to-measure curtains. The product costs $50 per square meter. Customers are asked to enter the width and height they need – in cm – but they will incur a minimum charge of $50 regardless of the total size being ordered.

You can achieve this with the following formula:

max( 50, ( [width] / 100 ) * ( [height] / 100 ) * [product_price] )

In the formula above, the function will return [width] * [height] * [product_price] if it is more than 50. Otherwise, it will always return 50, no matter how small the values for [width] and [height] are.

For example, when width is 100 and height is 50, the formula is calculated as follows:

max( 50, ( 100 / 100 ) * ( 50 / 100 ) * 50 ) = max( 50, 1 * 0.5 * 50 ) = max( 50, 25 ) = 50

Example: min

Use this to set a maximum price for the price formula.

A store sells guided tours of archeological sites. Tourists pay a fee of $10 per ticket but larger groups can get a deal because the tour is capped at a maximum charge of $400 (corresponding to 40 people) but can accommodate up to 50 people (so every person in that group exceeding 40 people gets to participate for free).

You can achieve this with the following formula:

min( 400, [tickets] * [product_price] )

In the formula above, the function will return [tickets] * [product_price] if it is less than 400. Otherwise, it will always return 400, no matter how large the value for [tickets] is.

For example, when the number of tickets is 48, the formula is calculated as follows:

min( 400, 48 * 10 ) = min( 400, 480 ) = 400

How to round prices

You can round the value of a calculated price by using function among round, trunc, floor and ceil, with the following specifications:

  • round approximates the value to the closest integer (e.g round( 3.37 ) returns 3; round( 5.98 ) returns 6).
  • floor returns the closest integer smaller than the input value (e.g floor( 3.37 ) returns 3; floor( 5.98 ) returns 5).
  • trunc truncates the decimal portion of a number (e.g. trunc( 3.37 ) returns 3; floor( 5.98 ) returns 5). With positive numbers, trunc and floor both return the same value.
  • ceil returns the closest integer greater than the input value (e.g floor( 3.37 ) returns 4; floor( 5.98 ) returns 6).

Example: ceil

A store sells pet food. Dog food is $28.15 per kg. Customers can enter the required weight with 0.1 kg precision. The shop owner wants prices to be calculated to the whole dollar, approximating by excess. The appropriate formula is:

ceil( [weight] * [product_price] )

For example, if 2.5 is entered as weight, the formula is calculated as follows:

ceil( 2.5 * 28.15 ) = ceil( 70.375 ) = 71

Example: round

A website provides housekeeping services and allows customers to book cleaners in 1-hour slots. The cost for 1 hour is $19.85 and the site owner wants to round the price calculation in $5 increments so that $19.85 returns $20 and $357.30 returns $355. The appropriate formula is:

5 * round( [hours] * [product_price] / 5 )

For example, if a customer requests 18 hours, the formula is calculated as follows:

5 * round( 18 * 19.85 / 5 ) = 5 * round( 71.46 ) = 5 * 71 = 355

User-defined functions

For more advanced uses, you can create your custom functions, which can then be used in price formulas like any other mathematical function. To learn more about user-defined functions, you can read How to define new custom functions.

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.