To include calculations in custom themes, particularly using numbers entered in custom fields, use arithmetic filters available in the Liquid templating language:
abs
— returns the absolute value of a numberat_least
— limits a number to a minimum valueat_most
— limits a number to a maximum valueceil
— rounds the input up to the nearest whole numberdivided_by
— divides a number by the specified numberfloor
— rounds a number down to the nearest whole numberminus
— subtracts a number from another numbermodulo
— returns the remainder of a division operationplus
— adds a number to another numberround
— rounds an input number to the nearest integer or, if a number is specified as an argument, to that number of decimal placestimes
— multiplies a number by another numberLet’s say you have two custom fields defined for sales invoices. Both are number type fields:
Field 1
Field 2
and you want to also show on invoices the sum of Field 1
and Field 2
. You do not need to define a third custom field and enter content into it. Instead, inject the following lines into your custom theme at the appropriate place, following the code that displays the custom_fields
array:
<div style="font-weight: bold; padding-top: 20px">Field 1 + Field 2</div>
<div>{{ custom_fields["Field 1"] | plus: custom_fields["Field 2"] }}</div>
The first line displays a label for your calculated result. The second includes the Liquid arithmetic filter that adds your two custom fields’ values. The result is:
You can also code a custom theme to perform calculations involving built-in fields. For instance, if you want to add the value of Field 1
to the total or balance due on your invoice, inject one of the following lines into your theme:
{{ table.totals["Total"] | plus: custom_fields["Field 1"] }}
or:
{{ table.totals["Balance due"] | plus: custom_fields["Field 1"] }}
Note
This Guide is not a comprehensive tutorial on arithmetic filtering, and certainly not on Liquid coding in general. It is meant only to illustrate the concept of using filters to perform calculations in custom themes. Additional Liquid code will probably be necessary to obtain desired results. Users are responsible for the performance of their own custom themes and should consult online syntax guides or hire competent programming support if necessary.
Subscribe to our newsletter and get exclusive product updates you won't find anywhere else straight to your inbox.