Set a Minimum Order Amount on a Gravity Form
Say you’re using a Gravity Form as an order form. People can purchase stolen kittens from your factory, but you want to make sure they’re going to make it worth your while. Therefor, you’ve set a minimum of $25 / order.
Each kitten has a different cost, and if they choose a bunch of cheap kittens, then you’ll charge them a minimum $25 anyway, no matter what the cost was.
This requires two pieces of code in Gravity Forms, first some Javascript and then some PHP in your functions.php file.
This information was generally gathered from this page and the wonderful support team at Gravity Forms, but here I’m presenting it exactly as you need to in order to get it all working: https://www.gravityhelp.com/documentation/article/gform_product_info/
Javascript
Gravity Forms recommends adding the following Javascript directly to your Form. So go into WordPress > Forms and edit the form you want to work this magic with. Once you’re in there, add an HTML field and in the Content area, add the following code:
<script type="text/javascript">
gform.addFilter( 'gform_product_total', function(total, formId){
//only apply logic to form ID 1
if(formId != 1)
return total;
if(total < 25)
total = 25;
return total;
} );</script>
Note where it says formId != 1
, you should change the 1 to whatever the ID of your form is. You can get the ID of the form by looking in the address bar of your browser when editing the form.
http://website.com/wp-admin/admin.php?page=gf_edit_forms&id=1
Here’s a screenshot of exactly where to put the code above.
That will take care of updating the total but only on the front end. So it’ll show users the updated price, but when it comes time to checkout via Stripe or PayPal or if you’re using Gravity Forms with Woocommerce, the actual total won’t be updated.
To do that, open up your functions.php file and paste in the following:
add_filter( 'gform_product_info', 'add_fee', 10, 3 );
function add_fee( $product_info, $form, $lead ) {
$minimumprice = 25;
$currentprice = $product_info['products'][1]['price'];
$currentprice = str_replace('$','',$currentprice);
$additionalfee = $minimumprice - $currentprice;
if ( $currentprice >= 0 ) {
$product_info = array(
'products' => array(
array(
'name' => 'Minimum Order Amount',
'price' => 25,
'quantity' => 1,
),
),
);
}
return $product_info;
}
That gets you all setup!
If you’re interested, I’ve got a similar method for Woocommerce, i.e. forcing a minimum cart amount while still allowing people to check out (and just paying the minimum instead).
Up Next: Set Minimum Order Amount in Woocommerce, but Still Allow Checkout by Forcing a Minimum Price