DescriptionUsageJavaScript VersionParametersJS ExamplePlacementSource CodePHP VersionParametersPHP ExamplePlacementSource Code
Description
This filter can be used to dynamically modify the formula of a number field calculation or calculated product field.
Usage
The gform_calculation_formula filter has both a JavaScript version and a PHP version. Both versions should be used.
The JavaScript version only overrides the calculation formula on the front-end.
12345gform.addFilter( 'gform_calculation_formula', function( formula, formulaField, formId, calcObj ) { // do stuff return formula;} );
The PHP version overrides the calculation formula when the calculation is rerun during submission using the field values saved in the entry.
12345add_filter( 'gform_calculation_formula', function( $formula, $field, $form, $entry ) { // do stuff return $formula;}, 10, 4 );
JavaScript Version
Parameters
formula string
The calculation formula.
formulaField Javascript Object
The current calculation field object. e.g.
1{"field_id":3,"formula":"{:1}+{:2}","rounding":""}
formId integer
The ID of the form in use.
calcObj Javascript Object
The calculation object.
JS Example
123456gform.addFilter( 'gform_calculation_formula', function( formula, formulaField, formId, calcObj ) { if ( formId == '10' && formulaField.field_id == '3' ) { formula += '+5'; } return formula;} );
Placement
Your code snippet can be placed in a HTML field on your form or in a theme custom JavaScript file.
Source Code
This filter is located in js/gravityforms.js
PHP Version
Parameters
$formula string
The calculation formula.
$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.
PHP Example
12345678add_filter( 'gform_calculation_formula', 'change_formula', 10, 4 );function change_formula( $formula, $field, $form, $entry ) { if ( $form['id'] == 10 && $field->id == 3 ) { $formula .= '+5'; } return $formula;}
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFCommon::calculate() in common.php