sum

Calculates the sum of all numeric values in an array. Non-numeric values are ignored.

{% assign total = prices | sum %}
{% log total %}

Syntax

{{ array | sum }}
Parameter
Description

array

Array of values to sum

Return Value

Returns a number — the sum of all numeric values in the array. Non-numeric values are skipped.

Examples

Sum order line item prices:

{% assign prices = order.line_items | map: "price" %}
{% assign total = prices | sum %}
{% log "Total: $" | append: total %}

Sum quantities:

{% assign quantities = line_items | map: "quantity" %}
{% assign total_items = quantities | sum %}
{% log "Total items: " | append: total_items %}

Sum values from a CSV import:

Calculate total weight:

Sum with mixed data (non-numeric values are ignored):

Output:

Notes

  • Non-numeric values (strings, nulls, etc.) are silently skipped

  • Values are parsed as floats, so decimal numbers are supported

  • Combine with map to sum a specific property from an array of objects

  • Returns 0 for an empty array

Last updated