PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Update price based on number input field

  1. 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...

    Posted 12 years ago on Saturday October 22, 2011 | Permalink
  2. I have figured out how to do this and am including my solution in case it benefits anyone else. To reiterate, this script is placed in the functions.php file and it does the following:

    Users enter a total valuation of property on the form (this is a standard number field). When this is entered, this script will calculate a "permit fee" (used-defined price field) based on the amount entered.

    It calculates a state surcharge fee (user-defined price field) which is .0005 times the total valuation of the property.

    It calculates a plan review fee (user-defined price field) that is a specific percentage of the calculated permit fee based on whether this is a commercial property (65%) or a residential property (35%).

    Finally, a credit card convenience fee of $3.00 (single product field with price of $3.00 and quantity disabled) is also applied. The script for this solution can be viewed here: http://www.pastie.org/2805522

    Posted 12 years ago on Thursday November 3, 2011 | Permalink
  3. Thanks for sharing jbeim. :)

    Posted 12 years ago on Thursday November 3, 2011 | Permalink
  4. Thank you for the help! I am needing users to enter prices and then have the total updated at the end with a 20% reduction. I think this will help me out a lot.
    I am extremely new to this and don't know how to get to the bare code of the form. Do you use Dreamweaver or another program to edit your code?

    Thanks,
    Ryan

    Posted 12 years ago on Wednesday January 11, 2012 | Permalink