I have a form which has a number input field that represents the "total market valuation of a property". I need to calculate a permit fee which I have defined as a single product (quantity disabled) based on the number that is entered here. I have tried to follow some of the post examples such as adding tax to the total and others showing how to use the pre_render filter, but I just can't seem to get it to use the value that's entered to update the "price" of my product field (permit fee). Here's a link to my form: http://box731.bluehost.com/~windommn/?page_id=2042 and here is the code I added to functions.php:
// update the "29" to the ID of your form
add_filter('gform_pre_render_29', 'add_total_script');
function add_total_script($form) {
$input = 'input_66';
$valuation += rgpost($input);
?>
<script type="text/javascript">
function gform_product_total(formId, valuation, total){
var fee = 0;
if (valuation <= 500){
fee = 21.00;
} elseif (valuation <= 2000){
fee = 21.00 + ((valuation - 500)/100) * 2.75;
} elseif (valuation <= 25000){
fee = 62.25 + ((valuation - 2000)/1000) * 12.50;
} elseif (valuation <= 50000){
fee = 349.75 + ((valuation - 2000)/1000) * 9.99;
} elseif (valuation <= 100000){
fee = 574.75 + ((valuation - 2000)/1000) * 6.25;
} elseif (valuation <= 500000){
fee = 887.25 + ((valuation - 2000)/1000) * 5.00;
} elseif (valuation <= 1000000){
fee = 2887.25 + ((valuation - 2000)/1000) * 4.25;
} elseif (valuation > 1000000){
fee = 5012.25 + ((valuation - 2000)/1000) * 2.75;
}
return total + fee;
}
</script>
<?php
return $form;
}
// update the "29" to the ID of your form
add_filter('gform_product_info_29', 'update_product_info', 10, 3);
function update_product_info($product_info, $form, $entry) {
$total = get_total($product_info);
$input = 'input_66';
$valuation += rgpost($input);
$fee = 0;
if ($valuation <= 500){
$fee = 21.00;
} elseif ($valuation <= 2000){
$fee = 21.00 + (($valuation - 500)/100) * 2.75;
} elseif ($valuation <= 25000){
$fee = 62.25 + (($valuation - 2000)/1000) * 12.50;
} elseif ($valuation <= 50000){
$fee = 349.75 + (($valuation - 2000)/1000) * 9.99;
} elseif (valuation <= 100000){
$fee = 574.75 + (($valuation - 2000)/1000) * 6.25;
} elseif ($valuation <= 500000){
$fee = 887.25 + (($valuation - 2000)/1000) * 5.00;
} elseif (valuation <= 1000000){
$fee = 2887.25 + (($valuation - 2000)/1000) * 4.25;
} elseif (valuation > 1000000){
$fee = 5012.25 + (($valuation - 2000)/1000) * 2.75;
}
$product_info['products']['permitfee'] = array(
'name' => 'Permit Fee', // name that will appear in PayPal and pricing summary tables
'price' => $fee, // amount of total tax
'quantity' => 1
);
return $product_info;
}
function get_total($products) {
$total = 0;
foreach($products["products"] as $product){
$price = GFCommon::to_number($product["price"]);
if(is_array($product["options"])){
foreach($product["options"] as $option){
$price += GFCommon::to_number($option["price"]);
}
}
$subtotal = floatval($product["quantity"]) * $price;
$total += $subtotal;
}
$total += floatval($products["shipping"]["price"]);
return $total;
}
Can anyone help me figure this out? I've tried so many different things that I feel like I'm running in circles...