Using Gravity Forms ‘gform_calculation_result’ PHP filter

The gform_calculation_result Gravity Forms PHP filter allows you to modify the result of a number field calculation or calculated product field.

Usage

add_filter('gform_calculation_result', 'modify_calculation_result', 10, 5);
function modify_calculation_result($result, $formula, $field, $form, $entry) {
    // your custom code here
    return $result;
}

Parameters

  • $result (float): The calculation result.
  • $formula (string): The formula after merge tags have been processed.
  • $field (Field Object): The calculation field currently being processed.
  • $form (Form Object): The form currently being processed.
  • $entry (Entry Object): The entry currently being processed.

More information

See Gravity Forms Docs: gform_calculation_result

Examples

Calculate a percentage

Calculate 10% of a number from field with ID 1:

add_filter('gform_calculation_result', 'calculate_percentage', 10, 5);
function calculate_percentage($result, $formula, $field, $form, $entry) {
    if ($form['id'] == 10 && $field['id'] == 3) {
        $number = (float) rgar($entry, '1');
        $result = $number * 0.1;
    }
    return $result;
}

Calculate the sum of two fields

Calculate the sum of fields with IDs 1 and 2:

add_filter('gform_calculation_result', 'calculate_sum', 10, 5);
function calculate_sum($result, $formula, $field, $form, $entry) {
    if ($form['id'] == 10 && $field['id'] == 3) {
        $num1 = (float) rgar($entry, '1');
        $num2 = (float) rgar($entry, '2');
        $result = $num1 + $num2;
    }
    return $result;
}

Calculate the difference between two fields

Calculate the difference between fields with IDs 1 and 2:

add_filter('gform_calculation_result', 'calculate_difference', 10, 5);
function calculate_difference($result, $formula, $field, $form, $entry) {
    if ($form['id'] == 10 && $field['id'] == 3) {
        $num1 = (float) rgar($entry, '1');
        $num2 = (float) rgar($entry, '2');
        $result = $num1 - $num2;
    }
    return $result;
}

Calculate the product of two fields

Calculate the product of fields with IDs 1 and 2:

add_filter('gform_calculation_result', 'calculate_product', 10, 5);
function calculate_product($result, $formula, $field, $form, $entry) {
    if ($form['id'] == 10 && $field['id'] == 3) {
        $num1 = (float) rgar($entry, '1');
        $num2 = (float) rgar($entry, '2');
        $result = $num1 * $num2;
    }
    return $result;
}