With Gravity Forms, you can easily create WordPress forms for selling simple products. One missing piece is the concept of inventory. While this snippet does not provide a full featured inventory solution, it will allow you to specify a product (via its field ID) and limit how many of that product can be ordered.
If the product is “sold out”, a message is displayed in place of the form indicating this. If the product is not sold out, but the user attempts to order more than you have “in stock”, a validation message is displayed on the product field letting the user know how many of that product are available.
<?php
/*** Simple Ticket Inventory with Gravity Forms / Limit by Sum of Field Values* http://gravitywiz.com/2012/05/19/simple-ticket-inventory-with-gravity-forms/*/
$sum_form_id = 86;$sum_field_id = 14.3;$sum_limit = 450;$sum_limit_message = 'Sorry, this show is sold out.';$sum_validation_message = 'You ordered %1$s tickets. There are only %2$s tickets left.';
add_filter("gform_pre_render_$sum_form_id", 'gform_limit_by_field_values');function gform_limit_by_field_values($form) { global $sum_field_id, $sum_limit, $sum_limit_message; $sum = gform_get_field_values_sum($form['id'], $sum_field_id); if($sum >= $sum_limit) add_filter('gform_get_form_filter', create_function('', 'return "' . $sum_limit_message . '";')); return $form;}
add_filter("gform_validation_$sum_form_id", 'gform_limit_by_field_values_validation');function gform_limit_by_field_values_validation($validation_result) { global $sum_field_id, $sum_limit, $sum_validation_message; $form = $validation_result['form']; $exceeded_limit = false; foreach($form['fields'] as &$field) { if($field['id'] != intval($sum_field_id)) continue; $requested_value = rgpost("input_" . str_replace('.', '_', $sum_field_id)); $field_sum = gform_get_field_values_sum($form['id'], $sum_field_id); if($field_sum + $requested_value <= $sum_limit) continue; $exceeded_limit = true; $number_left = $sum_limit - $field_sum >= 0 ? $sum_limit - $field_sum : 0; $field['failed_validation'] = true; $field['validation_message'] = sprintf($sum_validation_message, $requested_value, $number_left); } $validation_result['form'] = $form; $validation_result['is_valid'] = !$validation_result['is_valid'] ? false : !$exceeded_limit; return $validation_result;}
function gform_get_field_values_sum($form_id, $field_id) { global $wpdb; $sql = $wpdb->prepare("SELECT sum(value) FROM {$wpdb->prefix}rg_lead_detail WHERE form_id = %d AND CAST(field_number as unsigned) = %d", $form_id, $field_id); return $wpdb->get_var($sql);}How do I install this snippet?
Just copy and paste the code above into your theme’s functions.php file.
Do I need to configure this snippet to work with my form?
Yes, you do. All of the variables you will need to configure are right at the top.
- Update the
$sum_form_idto the ID of your form. - Update the
$sum_field_idto the ID of your product or quantity field. If using a Single Product field, you’ll need to specify the quantity input ID which will always be{field ID}.3(ie if the field ID is 12, the quantity input ID will be12.3). If using a separate Quantity field, simply specify the field ID (ie12). - Update the
$sum_limitto the number this product should be limited to. - Update the
$sum_limit_messageto the message which should be displayed to users when the product limit has been reached. You can get fancy with this and add HTML as well. Be sure to escape any double quotes with a backslash. Here’s a more advanced example:$sum_limit_message = '<div style="border: 1px solid #e6db55; background-color: #FFFFE0; padding: 10px;">Sorry, this show is sold out.</div>';
Simple Ticket Inventory: Limit Message - Update the
$sum_validation_messageto the validation message which should be displayed on the product field if the product limit has not been reached, but the user’s requested quantity would exceed the product limit.
Simple Ticket Inventory: Limit Validation Message
Anything else?
This example is centered around the concept of tickets/products; however, keep in mind that this snippet could be applied to anything where you need to limit submissions based on the sum of a field’s previously submitted data.
Any other ideas?
Hey. Very nice site! I was testing this snippet and I get error:
Call to undefined function gform_get_field_values_sum()
Any idea why?
Oops! Looks like snippet got clipped. It has been updated to include that required function as well. Let me know how it goes!
My test form (on two different sites, both up to date with GF) is complaining that
gform_get_field_values_sum() in your line 16 is undefined.Did I miss something?Hm, the code was updated yesterday to include this required function. You should be seeing this function ( http://grab.by/dKvi ) as the last bit of the snippet. Is this not displaying for you?
Yes, my browser seems to have cached the page a few days ago, and didn’t refresh until I posted my comment.
Thanks!
I got it working allright. This would be great feature also when using radio buttons for several items.
Hi Sami, so limiting the field rather than the form? Let me know what you had in mind.
Will the inventory count down for Payment Status “Approved” only or does “Processing” affect the amount of items left too?
Hi Allan, this version does not. It’s been improved here: http://gravitywiz.com/2012/09/19/better-inventory-with-gravity-forms/
Awesome. Thank you.
This brilliant piece of code saved me today, thank you! I wonder if it would be possible to modify so that it could work on more than one form at a time…
Hi Christina, check out the Better version of this snippet for the improved code and an easy method for adding this to multiple forms.