Programatically Update Grand Total?

I am trying to work some logic around to manipulate the grand total in a GravityForm form before it gets sent to the payment processor. I can’t for the life of me figure out how to do it though. I have a calculation field that dynamically sets itself to a discount, my issue is apparently actually getting the finaltotal to recognize this amount. The code for the calculation I have right now (with the switch case omitted):


// Custom Form Logic to Change Total Base on Membership Rates
add_filter( 'gform_calculation_result', function( $result, $formula, $field, $form, $entry ) {
    // do stuff

    if ($form['id'] == 28 && $field['id']== 107){
        $membershipLevel = $_COOKIE['memberlevel'];
        $discountAmount = setFieldsPHP($membershipLevel);



        print_r($result);
        if ($result >= $discountAmount){
            $result = $result - $discountAmount;
            print_r("FINAL TOTAL: " . $result . ' WITH DISCOUNT OF: ' . $discountAmount);
        }
        else {
            $result = ($result - $result) * -1;
            print_r('<BR> FINAL TOTAL SHOULDVE BEEN: 500 - ' . $result  . ' WITH DISCOUNT OF: ' . $discountAmount);
        }
    }
    return $result;
}, 10, 5 );

Which this “works”. The print_r’s display the right thing upon form submission. However I seem to be missing that final step that lets me actually apply this discount to the grand total and get it to effect. Any idea what i’m missing?

Thanks!

So I think I might have a idea what’s happening. I have some fields set up as product fields that are still adding to the total when they shouldn’t. Is there a way to have a product field that still shows a price and allows quantity without adding to the total overall?

Lois, product fields are intended to be added to the form total automatically, that’s their purpose. So if you don’t want this to happen, I would recommend you to replace those fields with Number fields instead. Number fields can be set to use Currency formatting, and you can also use their values in calculation formulas.

Also note that the gform_calculation_result filter has two versions, PHP and JS, you need to use both.

Thanks for the reply. How do you use their values in calculation formulas? Would I have a text both just showing the product description & the price and then a number box for quantity?

I’m using both versions of gform_calculation_result. It just seems like the actual total on the front end isn’t updating according to that. I have it working in PHP with this:

add_filter('gform_product_info_28', 'add_fee', 10, 3);

    function setProductPrice($productkey){
        switch($productkey){
            case 66:
            case 69:
            case 70:
            case 71:
            case 72:
            case 73:
            case 74:
            case 81:
            case 91:
            case 92:
            case 93:
            case 98:
            case 95:
            case 96:
            case 97:
            case 99:
            case 100:
            case 101:
                return True;
                break;
            default:
                return False;
                break;
        }
    }

    function add_fee($product_info, $form, $lead){
       foreach($product_info['products'] as $key => $product){

           $getPrice = setProductPrice($key);

//           $product['price'] = $getPrice == 0 ? $getPrice : $product['price'];

           if ($getPrice == True){
               $product_info['products'][$key]['price'] = 0;
           }

//           print_r('<br>'. $product['name'] . ' ' .$product['price']);
       }


       return $product_info;
    }

but I feel like this is a very hacky way of handling this. But it appears to work. Now I need to find a way to get the front end Total fields to update accordingly. Any ideas?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.