Reputation: 27
I am currently using this code to calculate tax on one of my forms, but I'd like to have another form calculate tax as well and I'm not sure how to edit this code so it effects 2 forms or if there is something better I can do???
// update the "78" to the ID of your form add_filter('gform_pre_render_78', 'add_total_script'); function add_total_script($form) { ?> function gform_product_total(formId, total){ var tax = ((5 * total) / 100); // update the "20" to the desired tax percentage; tax = Math.round(tax*100)/100; //rounding tax to 2 decimal digits return total + tax; } 'Tax', // name that will appear in PayPal and pricing summary tables 'price' => $tax, // 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; }
Upvotes: 0
Views: 1728
Reputation: 39
please check bellow code . Add javascript to view tax in front end
<script type="text/javascript">
gform.addFilter( 'gform_product_total', function(total, formId){
var tax = 10,
newtax = ( total * tax ) / 100 ;
return total + newtax;
} );
</script>
Now let's update the tax to database .
function saiful_gf_remove_money_symbol($s) {
return GFCommon::to_number( $s );
}
add_filter( 'saiful_gf_remove_money_symbol', 'saiful_gf_remove_money_symbol' );
function saiful_gf_to_money($s) {
return GFCommon::to_money( $s );
}
add_filter( 'saiful_gf_to_money', 'saiful_gf_to_money' );
add_filter( 'gform_product_info', 'add_fee', 10, 3 );
function add_fee( $product_info, $form, $lead ) {
$tax = 10;
$loop_price = 0;
if( isset($product_info['products']) && count($product_info['products']) > 0 ){
foreach ($product_info['products'] as $data){
if( isset($data['options']) && count($data['options']) > 0 ){
foreach ( $data['options'] as $key => $option ) {
$loop_price += floatval( $option['price'] );
}
}
$new_price = apply_filters( 'saiful_gf_remove_money_symbol',$data['price']) + $loop_price ;
$new_price = ( ( $new_price * $tax ) / 100 ) + apply_filters( 'saiful_gf_remove_money_symbol',$data['price']);
$data['price'] = apply_filters( 'saiful_gf_to_money', $new_price );
$product_info['products'] = $data;
}
}
return $product_info;
}
Thanks
Upvotes: 0
Reputation: 2869
Since the introduction of "calculations" in Gravity Forms (can't remember what version), this method for calculating tax is only required if the tax calculation is very complicated. It looks like your tax is pretty straight forward.
Here is an article that demonstrates the best way to add tax for your Gravity forms (and also provides a merge tag for generating the current subtotal):
http://gravitywiz.com/subtotal-merge-tag-for-calculations/
Make sure you check out the demo to see this in action.
Upvotes: 0