Create a calculated field
1
Open custom data settings
Go to Custom data and select Items, Cycles, or Consignors.
2
Add a calculated definition
Select Add definition on the Calculated fields card.
3
Set up the field
Enter a Name, the display name shown on the record, such as
Total rental cost. The Key is generated from the name and can be edited until you save. Set the Result type to the type the formula produces: single line text, multi line text, money, integer, boolean, date, or date and time. Enter the Formula as a Liquid expression.4
Save
Select Save.
The key and result type can’t be changed after creation. You can still change the name and formula.
Write formulas
Formulas use Liquid, the templating language Shopify themes and notifications use. A formula is any Liquid expression that renders to a value matching the field’s result type. The form lists the available variables as pills. Select one to insert it into the formula.General rules
- Formulas run in strict mode. Referencing an undefined variable makes the formula fail.
- Only scalar custom fields can be referenced. Reference fields (customer, cycle, item, return), color, URL, and JSON fields aren’t available in formulas.
- Built-in system fields (completed cycles, acquisition cost, schedule dates, charges, and similar) are available alongside custom fields. The formula picker lists them as pills.
- A calculated field can’t reference another calculated field.
- Money values are integers in cents.
$15.00is1500, and a money-typed formula must also produce cents. Multiply and divide accordingly when combining money with other numbers. - Durations from system fields are in days, for example
utilized_daysandtotal_utilized_days.
100:
Liquid basics
Liquid has two kinds of markup. Output,{{ ... }}, renders a value, and everything a formula produces has to end up inside output tags. Tags, {% ... %}, control logic and render nothing themselves, for example {% if %}, {% assign %}, and {% case %}.
Values are transformed with filters, chained with |:
Arithmetic
Arithmetic
plus, minus, times, divided_by, moduloRounding
Rounding
round, ceil, floorFallback
Fallback
default substitutes a value when the input is empty.Text
Text
upcase, downcase, capitalize, append, prependDates
Dates
date formats a date with a strftime string.Conditional logic
Use{% if %} to branch. The formula still has to render a value that matches the result type, so return one on every branch:
{% case %} is often cleaner:
Intermediate values
{% assign %} names intermediate values so complex formulas stay readable:
Liquid variable reference
The variables available in a formula depend on the calculated field’s owner. Custom field keys are shop-defined, so they’re shown asfields.<key> and metafields.<key>.
Scalar custom field types
Everyfields.<key> variable takes its Liquid type from the underlying custom field definition:
Liquid references by owner
Expand an owner to see every variable it exposes, with its Liquid type and where the value comes from.Item
Item
One entry per scalar custom field defined on items, keyed by the definition’s key. The Liquid type mirrors the custom field type:
money and integer are integers (money in cents), boolean is a boolean, and text, date, and date_time are strings.Share of the item’s published lifetime spent utilized, as a fraction from
0 to 1. 0 when the item has no published_at.Total days the item has been utilized, rounded to two decimal places. Fleet age is measured from
published_at when set.Total days the item has not been utilized since publish, rounded to two decimal places.
0 when the item has no published_at.Cycle
Cycle
One entry per scalar custom field defined on cycles, keyed by the definition’s key. The Liquid type mirrors the custom field type:
money and integer are integers (money in cents), boolean is a boolean, and text, date, and date_time are strings.The rented item. Includes the item’s custom fields, system fields, and, when the item has a linked Shopify variant, its variant data. Nested as
item.*, for example item.completed_cycles or item.acquisition_cost.Result types
A calculated field’s output must match its declared result type. The result types are the same scalar set as the referenceable custom field types:money (stored as cents, for example "4500"), integer, boolean, single_line_text_field, multi_line_text_field, date, and date_time.
Example formulas
Miles completed during a cycle (integer)
Set on a cycle. Record the item’s odometer reading at handover and return, and store the difference as the miles completed on this cycle.Total miles completed on an item (integer)
Set on an item. Accumulate miles across cycles by adding this cycle’s contribution to the running total already stored on the item. Because calculated fields recalculate on demand, select Recalculate on the item’stotal_miles field after each cycle to roll its value forward.
Average revenue per completed cycle (money)
Set on an item. Divide lifetime revenue by completed cycles. Money stays in cents.Time to breakeven, in cycles (integer)
Set on an item. Divide the remaining cost to recover by the average revenue per cycle, using system fields for acquisition cost and lifecycle revenue plus anaverage_revenue_per_cycle metafield you maintain on the product.
Has broken even (boolean)
Set on an item. Flag items whose lifecycle revenue has met or exceeded their acquisition cost.Cycle yield including charges (money)
Set on a cycle. Add the discounted line price to the charges paid on the cycle.Recalculate a value
Calculated fields don’t update on their own when the fields they depend on change.1
Open the record
Open the item, cycle, or consignor that owns the calculated field.
2
Find the calculated field
Scroll to the Custom fields section and find the field.
3
Recalculate
Select the field, then select Recalculate. The formula renders against the record’s current values and the result is saved.
If the formula is invalid, or the result doesn’t match the declared result type (for example
"abc" for a Money field), the recalculation fails and the stored value is left unchanged. Fix the formula under Custom data and try again.