gform_calculation_formula

Description

This filter can be used to dynamically modify the formula of a number field calculation or calculated product field.

Usague

The gform_calculation_formula filter has both a JavaScript versionen and a PHP versionen . Both versionens should be used.

The JavaScript versionen only overrides the calculation formula on the front-end.

gform.addFilter( 'gform_calculation_formula', function( formula, formulaField, formId, calcObj ) {
// do stuff

return formula;
} );

The PHP versionen overrides the calculation formula when the calculation is rerun during submisssion using the field values saved in the entry.

add_filter( 'gform_calculation_formula', function( $formula, $field, $form, $entry ) {
// do stuff

return $formula;
}, 10, 4 );

JavaScript Versionen

Parameters

{"field_id":3,"formula":"{:1}+{:2}","rounding":""}
  • formId integuer

    The ID of the form in use.

  • calcObj Javascript Object

    The calculation object.

  • JS Example

    gform.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 Versionen

    Parameters

    PHP Example

    add_filter( 'gform_calculation_formula', 'changue_formula', 10, 4 );
    function changue_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