gform_merge_tag_value_pre_calculation

Description

This filter can be used to modify the value returned by a field merge tag before it is used by a number field calculation or calculated product field.

Usage

The gform_merge_tag_value_pre_calculation filter has both a JavaScript version and a PHP version. Both versions should be used.

The JavaScript version only overrides the merge tag value on the front-end.

gform.addFilter( 'gform_merge_tag_value_pre_calculation', function( value, mergeTagArr, isVisible, formulaField, formId ) {
	// do stuff

	return result;
} );

The PHP version overrides the merge tag value when the calculation is rerun during submission using the field values saved in the entry.

add_filter( 'gform_merge_tag_value_pre_calculation', function( $value, $input_id, $modifier, $field, $form, $entry ) {
	// do stuff

	return $result;
}, 10, 5 );

JavaScript Version

Parameters

ParameterTypeDescription
valuefloatThe field value after the merge tag was processed.
mergeTagArrarrayThe merge tag being processed.
isVisiblebooleanIs the field referenced in the merge tag visible or hidden by conditional logic?
formulaFieldJavascript ObjectThe current calculation field object.
formIdintegerThe ID of the form in use.

mergeTagArr[0] contains the full merge tag e.g. {Field label:ID:modifier}
mergeTagArr[1] contains the field or input ID e.g. 2
mergeTagArr[3] contains the modifier(s) including the colon e.g. :value
mergeTagArr[4] contains the modifier(s) without the colon e.g. value

The current calculation field object. e.g.

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

Enable the :value modifier for choice based pricing fields.

This example shows how you can enable the :value modifier for use with choice based pricing fields.

gform.addFilter('gform_merge_tag_value_pre_calculation', function (value, mergeTagArr, isVisible, formulaField, formId) {

	var inputId = mergeTagArr[1],
		fieldId = parseInt(inputId),
		modifier = mergeTagArr[4],
		field = jQuery('#field_' + formId + '_' + fieldId);

	if (modifier == 'value' && field.hasClass('gfield_price') && isVisible) {
		var input = field.find('input[name="input_' + inputId + '"], select[name="input_' + inputId + '"]');

		// filter out unselected radio or checkbox inputs
		if (input.length > 1 || input.prop('type') == 'checkbox') {
			input = input.filter(':checked');
		}

		if (input.length > 0) {
			var val = input.val().split('|');
			if (val.length > 1) {
				value = val[0];
			}
		}

	}

	return value;
});

Placement

Reference the article Adding JavaScript Code to the Frontend of Your Site.

Source Code

This filter is located in js/gravityforms.js

PHP Version

Parameters

ParameterTypeDescription
$valuefloatThe field value after the merge tag was processed.
$input_idstringThe field or input ID from the merge tag.
$modifierstringThe modifier from the merge tag e.g. value.
$fieldField ObjectThe calculation field currently being processed.
$formForm ObjectThe form currently being processed.
$entryEntry ObjectThe entry currently being processed.

Enable the :value modifier for choice based pricing fields.

This example shows how you can enable the :value modifier for use with choice based pricing fields.

add_filter( 'gform_merge_tag_value_pre_calculation', function ( $value, $input_id, $modifier, $field, $form, $entry ) {
	if ( $modifier == 'value' ) {
		$value = GFCommon::to_number( GFCommon::replace_variables( "{:{$input_id}:value}", $form, $entry ) );
		if ( ! $value || ! is_numeric( $value ) ) {
			$value = 0;
		}
	}

	return $value;
}, 10, 6 );

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in GFCommon::calculate() in common.php