Tutorial: Examples of Complex Form Calculations Formulas for Multi-Valued Fields
The Technolutions KB has a number of great resources on using Form Calculations:
Calculated Fields in Forms
Reader Review Form Calculations
Using Conditional Logic in Form Calculations
But putting them all into practice can be tricky. This short tutorial will combine the instructions of Calculated Fields in Forms (Multi-Valued Fields)s with the brief instructions on using ternary conditionals to create bulk discounts for course registrations.
Goal:
Create a single registration form to sign up for multiple courses that require payment. Example: Course Payment Calculation Examples
Note - this design works with both Check Boxes fields or Related Events widgets.
1) Configure your multiselect checkbox field or related events widget
The important thing is to always put a value in the Export Key field:
NOTE - If you're adding a Related Events widget to a Form and not an event, the Start and End date used to calculate "show/hide Related Events" will both default to Today's Date - so always use Date Range Start of 0 and configure your Date Range End value accordingly.
2) (Optional) Create a hidden Data Type = Real field to leverage the "Count of items selected" as a filter, perย Using Conditional Logic in Form Calculations.
The key calculation formula structure is @fieldName_list.length (per Calculated Fields in Forms - Multi Valued Fields)
This value will be used to show/hide other elements of the form using Conditional Logic filters, such as an instruction block that only appears when Count=8:
Or to more easily control dynamic liquid markup in the form communication emails:
{% if count = 8 %}Thank you for signing up for all 8 courses! {% else %} You have signed up for {{count}} courses. {% endif %}
3) Use Payment Form (or Payment Amount) widget with an amount due calculation in the Amount / Formula field
For Payment Form, you need to change the Payment Amount field to "Amount / Formula" to see the calculation field.
Payment Amount is an alternative to display an amount value which you'll use as a merge field elsewhere, but does not create anย amount due on the registration.
4) Add a ternary conditional to the calculation formula for bulk discounts
Ternary conditional is a JavaScript way to use a basic IF... THEN... ELSE structure inside of the Calculation Formula itself:
(IF ? THEN : ELSE)
Two practical examples for our form:
A) Buy 8 courses, pay a flat bundle rate
The "50" is the flat rate in the THEN clause.
(@pets_list.length == 8 ? 50 : @pets_list.length*10)
B) Buy at least 5 courses and get 10% off
In this one, there's a full calculation formula in the "THEN" clause.
(@pets_list.length >= 5 ? @pets_list.length*10*0.9 : @pets_list.length*10)
No comments to display
No comments to display