CartThrob 9 documentation for ExpressionEngine commerce teams Visit CartThrob.com
CartThrob.com

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:

  1. expression mode (expression="...")
  2. 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

  1. If expression is not provided, arithmetic uses num1, num2, and operator.
  2. In expression mode, debug returns the expression string instead of evaluation.
  3. If expression contains unparsed EE variables/tags, evaluation fails with a parse-order error.
  4. Raw output is numeric by default.
  5. format="yes" formats final output using number-format settings.
  6. Evaluation errors return parser messages when show_errors is 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"}