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()
Example formulas:
| Product | Formula |
|---|---|
| Garage door seal | width + (2 * height) |
| Circular area | 3.14159 * radius * radius |
| Box volume with walls | (length + 2) * (width + 2) * height |
| Rounded-up tile count | ceil(area / tile_size) |
| Minimum of two values | min(length, width) * price_factor |
Supported operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | length + width |
- | Subtraction | total - discount |
* | Multiplication | length * width |
/ | Division | area / coverage |
() | Grouping | (length + width) * 2 |
Supported functions
| Function | Description | Example |
|---|---|---|
min(a, b) | Returns the smaller value | min(length, width) |
max(a, b) | Returns the larger value | max(length, 10) |
ceil(x) | Rounds up to the nearest integer | ceil(area / 4) |
floor(x) | Rounds down to the nearest integer | floor(volume) |
round(x) | Rounds to the nearest integer | round(weight * 2.2) |
sqrt(x) | Square root | sqrt(area) |
abs(x) | Absolute value | abs(length - width) |
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.
Common test panel errors
| Error | Cause | Fix |
|---|---|---|
| Undefined variable | Formula references a variable not in the table | Add the variable or fix the name |
| Division by zero | A variable is zero and used as a divisor | Add a min value constraint > 0 |
| Invalid expression | Syntax error in the formula | Check for missing operators or parentheses |
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.
How to configure:
- Set Input Type to Dropdown.
- In the Options column, enter comma-separated values (e.g.,
4, 6, 8, 10, 12).
Hybrid input
A dropdown with a "Custom..." option that reveals a free input field.
How to configure:
- Set Input Type to Hybrid.
- In the Options column, enter your preset values.
- When a customer selects "Custom...", a number input appears.
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:
Variables:
| Name | Label | Unit | Min | Max |
|---|---|---|---|---|
width | Door Width | ft | 6 | 20 |
height | Door Height | ft | 6 | 12 |
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:
Variables:
| Name | Label | Unit | Min | Max |
|---|---|---|---|---|
diameter | Table Diameter | in | 24 | 96 |
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:
Variables:
| Name | Label | Unit | Min | Max |
|---|---|---|---|---|
length | Room Length | ft | 1 | 100 |
width | Room Width | ft | 1 | 100 |
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_widthis clearer thanx. - 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 heightshould bewidth * 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:
Widthis different fromwidth. - Remove any spaces from variable names.