chrishajer

apply price changes to multiple forms

Sep 6th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/adding-taxes-to-total
  2. add_action('gform_pre_render', 'add_surcharge_js');
  3. function add_surcharge_js($form){
  4.  
  5.     // change the 9 here to your second form id.  Use as many form IDs as you want
  6.     $formidscript = array(6,9);
  7.     // if this form is not in our array of $formidscript, return $form info unchanged
  8.     if(!in_array($form['id'], $formidscript))
  9.     return $form;
  10.     // otherwise, inject this script
  11.  
  12.     ?>
  13.     <script type="text/javascript">
  14.         function gform_product_total(formId, total){
  15.             if(total == 0)
  16.                 return total;
  17.             return total + (total * 0.039);
  18.         }
  19.     </script>
  20.     <?php
  21.     return $form;
  22. }
  23.  
  24. add_filter("gform_product_info", "add_surcharge", 10, 3);
  25. function add_surcharge($product_info, $form, $lead){
  26.  
  27.     // change the 9 here to your second form id.  Use as many form IDs as you want
  28.     $formidcharge = array(6,9);
  29.     // if this form is not in our array of $formidcharge, return $product_info unchanged
  30.     if(!in_array($form['id'], $formidcharge))
  31.         return $product_info;
  32.     // otherwise, apply the price change to the front end
  33.  
  34.     $total = get_total($product_info);
  35.     $fee = ($total * 0.039);
  36.     $product_info["products"]["surcharge"] = array("name" => "Surcharge", "price" => $fee, "quantity" => 1);
  37.  
  38.     return $product_info;
  39. }
  40.  
  41. function get_total($products) {
  42.     $total = 0;
  43.     foreach($products["products"] as $product){
  44.  
  45.         $price = GFCommon::to_number($product["price"]);
  46.         if(isset($product["options"]) && is_array($product["options"])){
  47.             foreach($product["options"] as $option){
  48.                 $price += GFCommon::to_number($option["price"]);
  49.             }
  50.         }
  51.         $subtotal = floatval($product["quantity"]) * $price;
  52.         $total += $subtotal;
  53.     }
  54.     $total += floatval($products["shipping"]["price"]);
  55.     return $total;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment