Guides

Include calculations in custom themes

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 number
  • at_least — limits a number to a minimum value
  • at_most — limits a number to a maximum value
  • ceil — rounds the input up to the nearest whole number
  • divided_by — divides a number by the specified number
  • floor — rounds a number down to the nearest whole number
  • minus — subtracts a number from another number
  • modulo — returns the remainder of a division operation
  • plus — adds a number to another number
  • round — rounds an input number to the nearest integer or, if a number is specified as an argument, to that number of decimal places
  • times — multiplies a number by another number

Example 1

Let’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:

rr9LRJE

Example 2

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.

Support
Subscribe to Updates

Subscribe to our newsletter and get exclusive product updates you won't find anywhere else straight to your inbox.

© 2018 - Based in Sydney, Australia but providing goodness globally