Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://www.gravityhelp.com/forums/topic/adding-taxes-to-total
- add_action('gform_pre_render', 'add_surcharge_js');
- function add_surcharge_js($form){
- // change the 9 here to your second form id. Use as many form IDs as you want
- $formidscript = array(6,9);
- // if this form is not in our array of $formidscript, return $form info unchanged
- if(!in_array($form['id'], $formidscript))
- return $form;
- // otherwise, inject this script
- ?>
- <script type="text/javascript">
- function gform_product_total(formId, total){
- if(total == 0)
- return total;
- return total + (total * 0.039);
- }
- </script>
- <?php
- return $form;
- }
- add_filter("gform_product_info", "add_surcharge", 10, 3);
- function add_surcharge($product_info, $form, $lead){
- // change the 9 here to your second form id. Use as many form IDs as you want
- $formidcharge = array(6,9);
- // if this form is not in our array of $formidcharge, return $product_info unchanged
- if(!in_array($form['id'], $formidcharge))
- return $product_info;
- // otherwise, apply the price change to the front end
- $total = get_total($product_info);
- $fee = ($total * 0.039);
- $product_info["products"]["surcharge"] = array("name" => "Surcharge", "price" => $fee, "quantity" => 1);
- return $product_info;
- }
- function get_total($products) {
- $total = 0;
- foreach($products["products"] as $product){
- $price = GFCommon::to_number($product["price"]);
- if(isset($product["options"]) && 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment