Custom Formulas

Custom Formulas

The custom formula builder lets you create any measurement calculation using variables, math operators, and functions. If the built-in calculator types don't fit your product, a custom formula will.

Overview

Custom formulas work by:

  • You define variables (e.g., width, height, radius).
  • You write a formula using those variables (e.g., width + (2 * height)).
  • Customers enter values for each variable on the product page.
  • The plugin evaluates the formula and calculates the price.

Setting up a custom formula

  • Go to the Measurement tab in any product.
  • Select Custom Formula as the calculator type.
  • Set the Pricing Unit (e.g., "units", "sq ft", or any label that makes sense).

Adding variables

In the Custom Variables section:

  • Click Add Variable.
  • Enter a Variable Name (e.g., width). Use lowercase letters and underscores only.
  • Set the Label (e.g., "Width"). This is what customers see.
  • Set the Unit (e.g., "ft", "in", "cm"). Displayed next to the input.
  • Optionally set Min, Max, and Step constraints.
  • Choose an Input Type: Free, Dropdown, or Hybrid.
  • Repeat for each variable your formula needs.

Writing the formula

In the Formula field, write your expression using:

  • Variable names you defined
  • Math operators: +, -, *, /
  • Parentheses for grouping: (, )
  • Math functions: min(), max(), ceil(), floor(), round(), sqrt(), abs()
ProductFormula
Garage door sealwidth + (2 * height)
Circular area3.14159 * radius * radius
Box volume with walls(length + 2) * (width + 2) * height
Rounded-up tile countceil(area / tile_size)
Minimum of two valuesmin(length, width) * price_factor
Scroll to see all columns →

Supported operators

OperatorDescriptionExample
+Additionlength + width
-Subtractiontotal - discount
*Multiplicationlength * width
/Divisionarea / coverage
()Grouping(length + width) * 2
Scroll to see all columns →
Standard math precedence applies: multiplication and division before addition and subtraction. Use parentheses to control order.

Supported functions

FunctionDescriptionExample
min(a, b)Returns the smaller valuemin(length, width)
max(a, b)Returns the larger valuemax(length, 10)
ceil(x)Rounds up to the nearest integerceil(area / 4)
floor(x)Rounds down to the nearest integerfloor(volume)
round(x)Rounds to the nearest integerround(weight * 2.2)
sqrt(x)Square rootsqrt(area)
abs(x)Absolute valueabs(length - width)
Scroll to see all columns →
Functions can be nested: ceil(sqrt(area) * 2)

Live test panel

The formula builder includes a test panel to preview calculations before publishing.

  • After defining variables and writing a formula, the test panel appears.
  • Enter sample values for each variable.
  • Click Calculate to see the result.
  • If the formula has errors, a red error message appears instead.
The test panel helps you catch mistakes before customers see the product.

Common test panel errors

ErrorCauseFix
Undefined variableFormula references a variable not in the tableAdd the variable or fix the name
Division by zeroA variable is zero and used as a divisorAdd a min value constraint > 0
Invalid expressionSyntax error in the formulaCheck for missing operators or parentheses
Scroll to see all columns →

Input types

Each variable can use one of three input types:

Free input

A standard number field. Customers type any value within the min/max range.

Best for: Measurements that vary widely (room dimensions, fabric lengths).

Dropdown input

A select menu with predefined values. Customers pick from your list.

  • Set Input Type to Dropdown.
  • In the Options column, enter comma-separated values (e.g., 4, 6, 8, 10, 12).
Best for: Standard sizes (sheet sizes, pipe diameters, common lengths).

Hybrid input

A dropdown with a "Custom..." option that reveals a free input field.

  • Set Input Type to Hybrid.
  • In the Options column, enter your preset values.
  • When a customer selects "Custom...", a number input appears.
Best for: Products with common sizes but custom sizing available (window blinds, countertops).

Formula validation

The plugin validates formulas in two places:

  • Admin (JavaScript) - Validates as you type. Green border means valid, red border means error, yellow border means a referenced variable is missing.
  • Server-side (PHP) - Validates again when the cart calculates the price. This prevents manipulation of client-side values.

Using numbers in formulas

You can use numeric constants alongside variables:

3.14159 * radius * radius
width + 2.5
ceil(area / 32)

Decimal numbers and integers are both supported.

Example: garage door seal

A garage door seal needs material for the width plus both sides:

NameLabelUnitMinMax
widthDoor Widthft620
heightDoor Heightft612
Scroll to see all columns →
Formula: width + (2 * height)

Pricing unit: ft

Result: A 16 ft wide, 7 ft tall door → 16 + (2 × 7) = 30 ft of seal material.

Example: circular table cover

A circular table cover priced by area:

NameLabelUnitMinMax
diameterTable Diameterin2496
Scroll to see all columns →
Formula: 3.14159 * (diameter / 2) * (diameter / 2)

Pricing unit: sq in

Result: A 48 in table → 3.14159 × 24 × 24 = 1,809.56 sq in.

Example: tile count

Calculate the number of tile boxes needed:

NameLabelUnitMinMax
lengthRoom Lengthft1100
widthRoom Widthft1100
Scroll to see all columns →
Formula: ceil((length * width) / 25)

This divides the room area by 25 sq ft (the coverage per box) and rounds up.

Pricing unit: boxes

Result: A 12 ft × 15 ft room = 180 sq ft → ceil(180 / 25) = ceil(7.2) = 8 boxes.

Best practices

  • Keep formulas simple. If a built-in type works, use it instead.
  • Set min/max constraints on every variable to prevent extreme values.
  • Test with edge cases. Try minimum values, maximum values, and zero.
  • Use descriptive variable names. door_width is clearer than x.
  • Use the test panel before publishing to verify your formula.
  • Document your formula in the product description so customers understand the calculation.

Troubleshooting

Formula shows "Invalid expression"

  • Check for missing operators between values (e.g., width height should be width * height).
  • Check for unmatched parentheses.
  • Make sure function names are spelled correctly.

Result is different from expected

  • Check operator precedence. Use parentheses to be explicit.
  • Verify variable names match exactly (case-sensitive).
  • Test with known values in the test panel.

Variable not recognized

  • Check spelling matches exactly between the variable name and the formula.
  • Variable names are case-sensitive: Width is different from width.
  • Remove any spaces from variable names.