Arithmetic
arithmetic performs inline math in templates and returns either a raw numeric result or a formatted display value.
You can use it in two modes:
- expression mode (
expression="...") - operator mode (
num1,num2,operator)
This tag is useful for template-level calculations such as subtotals, discounts, or presentation-only math.
See also:
Parameters
expression
Full arithmetic expression to evaluate.
When this parameter is present, num1/num2/operator mode is not used.
expression="{quantity} * {price}"
operator
Operator used in num1/num2 mode.
Supported values in code: +, -, *, /, %, ++, --.
Default: +.
operator="*"
num1
First number in num1/num2 mode.
Numeric input is sanitized before evaluation.
num1="{price}"
num2
Second number in num1/num2 mode.
Required for normal binary operators (+, -, *, /, %). For ++ and --, code sets this to 1 internally.
num2="{quantity}"
debug
Expression-debug helper.
When expression is used and debug is truthy, the tag returns the expression string instead of evaluating it. This helps diagnose parse-order issues.
debug="yes"
format
When truthy, the final numeric result is passed through ee()->number->format(...).
Use this for display output. For logic/comparisons, leave formatting off.
format="yes"
show_errors
Controls whether math parser errors are returned in output.
Default: yes/true.
If set false and evaluation fails, the result is not replaced by the parser message.
show_errors="no"
Behavior and Constraints
- If
expressionis not provided, arithmetic usesnum1,num2, andoperator. - In expression mode,
debugreturns the expression string instead of evaluation. - If expression contains unparsed EE variables/tags, evaluation fails with a parse-order error.
- Raw output is numeric by default.
format="yes"formats final output using number-format settings.- Evaluation errors return parser messages when
show_errorsis true.
Examples
Expression mode:
Item Subtotal: {exp:cartthrob:arithmetic expression="{item:quantity} * {item:price}"}
Expression mode with formatted output:
Item Subtotal: {exp:cartthrob:arithmetic expression="{item:quantity} * {item:price}" format="yes"}
Operator mode:
Item Subtotal: {exp:cartthrob:arithmetic operator="*" num1="{item:quantity}" num2="{item:price}"}
Modulo example:
Remainder: {exp:cartthrob:arithmetic operator="%" num1="10" num2="3"}
Debug expression string (parse-order troubleshooting):
{exp:cartthrob:arithmetic expression="{item:quantity} * {item:price}" debug="yes"}