spivurno
12/25/2016 - 1:01 PM

Gravity Perks // GP Subtotal & Tax // Dynamically Populate Tax Amount from Query String

Gravity Perks // GP Subtotal & Tax // Dynamically Populate Tax Amount from Query String

<?php
/**
 * Gravity Perks // GP Subtotal & Tax // Dynamically Populate Tax Amount from Query String
 * http://gravitywiz.com/
 */

add_filter( 'gform_pre_render',            'my_populate_tax', 8 );
add_filter( 'gform_pre_validation',        'my_populate_tax', 8 );
add_filter( 'gform_pre_submission_filter', 'my_populate_tax', 8 );

function my_populate_tax( $form ) {

    $form_id = 776;
	$tax_field_id = 7;
	$parameter    = 'tax';

	if( $form['id'] != $form_id ) {
	    return $form;
    }

	foreach( $form['fields'] as &$field ) {
		if( $field->id == $tax_field_id ) {
			$field->taxAmount = rgget( $parameter );
			$field->calculationFormula = sprintf( '( {subtotal} - {coupons} ) * ( %d / 100 )', rgget( $parameter ) );
			break;
		}
	}

	return $form;
}