Creating Coupons for GF Coupons Add-on with Gravity Forms
Dynamically create coupon codes for Gravity Forms which can be provided to the user for use on a subsequent form.
April 11, 2024: Added support for the required_fields
parameter to prevent a coupon from being created if one or more required fields are not completed.
July 22, 2021: Fixed an issue where scheduled coupons used GMT instead of the Time Zone configured in WordPress.
October 23, 2020: Migrated snippet to the Snippet Library.
July 8, 2020: Updated to support anonymous functions for any meta parameter.
December 29, 2018: Added support for passing a callable function for the "amount" parameter.
June 25, 2015: Updated to work with GF Coupon Add-on v2.0 and greater.
April 2, 2015: Updated snippet so that no coupon is created if no code is available.
January 12, 2015: Fixed issue where coupon code was not auto-uppercased per GF requirements
Submitting a Gravity Form is a powerful thing. After the the submit button is pushed, any number of things could happen. You could register the submitter as a user on your site. You could sign then up to your newsletter or drip campaign. You could sell them a product.
Sometimes users need a little extra push to fill out the form and hit that submit button. This snippet provides a way to dynamically create coupon codes which can be provided to the user for use on a subsequent form. A simple, compelling incentive to submit your form. To share your site. To sign up for your newsletter. To buy your products.
This snippet also supports creating coupons for Easy Digital Downloads and WooCommerce. Check out these awesome articles:
- Creating Coupons for Easy Digital Downloads with Gravity Forms
- Creating Coupons for WooCommerce with Gravity Forms
Getting Started
Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file. Having trouble installing the snippet? Read this.
Configure the snippet
- The basic configuration only requires that you specify the which form should be used to generate coupons (
form_id
), which field’s value should be used as the coupon code (source_field_id
), and thetype
andamount
of the coupon. - See the Usage Examples and available Parameters below.
- The basic configuration only requires that you specify the which form should be used to generate coupons (
This snippet allows you to dynamically create Gravity Form coupons; however, it doesn’t handle actually generating unique coupon codes. Try the GP Unique ID plugin for a simple and easy way to generate unique IDs that can be used with this snippet to create flexible, unique coupon codes.
Usage Examples
Gravity Form Coupon with Flat Discount, Applied to Cart
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'flat'
) );
Creates a flat $15 discount that applies to the entire cart. Whenever form ID 608 is submitted, the value of field ID 1 is used to create a new coupon.
Gravity Form Coupon with Percentage Discount, Applied to Cart
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'percentage'
) );
Creates a 15% discount that applies to the entire cart.
Stackable Gravity Form Coupon with Usage Limit and Expiration Date
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'flat',
'meta' => array(
'form_id' => 620,
'coupon_stackable' => true,
'coupon_limit' => 10,
'coupon_expiration' => '12/31/2015'
)
) );
Creates a flat $15 discount that applies to the entire cart. This coupon can be used with other coupons (we set coupon_stackable to true
). The coupon can be used up to 10 times (handled by the coupon_limit) and will expire on December 31, 2015 (via the coupon_expiration).
Gravity Form Coupon with Usage Limit and Name Set by Field Value
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'name_field_id' => 20,
'plugin' => 'gf',
'type' => 'percentage',
'amount' => 15,
'meta' => array(
'coupon_stackable' => false
'coupon_limit' => function() {
return rgpost('input_21');
},
),
) );
Creates a 15% discount that applies to the entire cart. This coupon’s title is derived from the value in Field 20 (handled by name_field_id). Its limit is set by a field value (handled by the function inside coupon_limit).
Gravity Form Coupon with unlimited use during the month of December 2015
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'flat',
'meta' => array(
'form_id' => 620,
'coupon_stackable' => false,
'coupon_limit' => false,
'coupon_start' => '12/1/2015',
'coupon_expiration' => '12/31/2015'
)
) );
Creates a flat $15 discount that applies to the entire cart. This coupon cannot be used with other coupons (we set coupon_stackable to false
) but it can be used an unlimited number of times (handled by setting coupon_limit to false
) during the month of December 2015 (via the coupon_start and coupon_expiration).
Parameters
Here is a full list of the available parameters and additional information on how each can be configured.
new GW_Create_Coupon( array(
'form_id' => 123,
'source_field_id' => 4,
'name_field_id' => 5,
'plugin' => 'gf',
'amount' => 15,
'type' => 'flat', // accepts: 'flat', 'percentage'
'meta' => array(
'form_id' => false,
'coupon_start' => '', // MM/DD/YYYY
'coupon_expiration' => '', // MM/DD/YYYY
'coupon_limit' => false,
'coupon_stackable' => false
),
'required_fields' => array( 6, 7, 8 )
) );
form_id (integer) (required)
The ID of the form which will be used to create coupons on submission.
source_field_id (integer) (required)
The ID of the field whose value will be used as the coupon code (a Unique ID field powered by GP Unique ID works great!).
name_field_id (integer) (optional)
The ID of the field whose value will be used as the coupon title.
plugin (string) (required)
The plugin for which you would like to generated a coupon. WooCommerce (
'wc'
), Easy Digital Downloads ('edd'
), and Gravity Forms ('gf'
) are currently supported.amount (integer|float) (required)
The amount the generated coupon should discount.
type (string) (required)
The type of coupon. Supported values are:
'flat'
Applies a flat discount to the entire cart. 'percentage'
Applies a percentage discount to the entire cart. meta (array) (optional)
An array of additional options that can be used to customize the generated coupon.
form_id The ID of the form which the coupon should be usable on. coupon_start The coupon start date. Date format must be MM/DD/YYYY. Leave blank if there is no coupon start date. coupon_expiration The coupon expiration date. Date format must be MM/DD/YYYY. Leave blank if there is no coupon expiration date. coupon_limit Set a number value for how many times this coupon can be used. Defaults to false which will allow it to be used an unlimited number of times. Accepts a function to use a field’s value. coupon_stackable Set whether the coupon can be used with other coupons. Set to true to allow coupon stacking. Defaults to false. required_fields (array) (optional)
An array of field IDs that must be submitted with a value in order for a coupon to be generated.
Create scannable coupon codes with GP QR Code.
With Gravity Forms QR Code, users can scan a QR code at checkout and apply a coupon, or you can direct them to a special checkout link with a specific product in their “cart” and the coupon already applied. Learn more about Gravity Forms QR Code.
How’d we do?
If you use it and like it, let us know. We’d love to hear the different ways you found this code useful!
PS—From here, creating multiple coupons at a time is only one snippet away. Check out How to Generate Bulk Coupon Codes with Gravity Forms to learn how.
Is there a way to make 1 time discount,
Meaning let’s say the offer is monthly payment 150$ per month, and for 1 month – I want to give 50% discount (only for 1st month, how do I do that?
Hi Ed,
This may be possible with the Discount field feature of our GP eCommerce field perk. Please submit a support ticket for this so we can have a look at your setup to confirm how you would use the Discount field for your use case.
Best,
Hey There
Is there a way I can update a coupon amount. For example, if I have a $10 coupon and I want to change the amount to $20, how can I do this with a snippet.
My idea is to adjust the coupon amount based on the number of items sold in a form.
We’ve already followed up via email. This should be possible by adding a function to the amount parameter:
'amount"' = function() { return rgpost( 'input_3' ); } // Update 3 with your field ID.
Best,
Hi,
I just have a question about these coupons saved location, can you tell me where do these coupons get saved in the backend (for example where in the cpanel of the website)
H Sahar,
You can find the coupons on the coupons page, which is a submenu under the Forms menu on the WP Admin backend.
Best,
Do you only need gravity forms purchased or do you also need gravity perks?
As it is not working with gravity forms alone.
Hi Annabelle,
You actually do not need Gravity Perks for this. The snippet works with the Gravity Forms Coupon Addon, so you only need Gravity Forms for this. Here’s an article on how to install snippets.
I hope this helps.
Best,
Buenas tardes;
Como puedo hacer en este código para que me devuelva el campo fecha compatible con el addon coupon de Gravity Forms con el formato fecha YYYY-MM-DD.
Es decir, necesito obtener el formato fecha YYYY-MM-DD para que el plugins lo pueda reconocer.
Gracias.
Hi Joseo,
You should use the default date format as it seems Gravity Forms Coupons Add-on only accepts YYYY-MM-DD format. I would suggest getting in touch with Gravity Forms support in case there’s a way to change the default format for the Coupons dates.
Best,
I am getting the error “Class ‘GW_Create_Coupon’ not found in wp-content/themes/divi_child/functions.php:627
I am using the Divi theme from Elegant Themes and the Divi Child theme on WordPress.
Thanks for publishing this, it’s great! 👍
I’ve been trying to figure out how to pass a dynamic parameter value.
Instead of setting amount to “10”, is it possible to set the amount based on a submitted field’s value? For example;
‘amount’ => rgar( $entry, ‘4’ ) ?
Hey Mike, This might be possible with something like $amount = $entry[‘4’]; but also may require some custom code to fully work. If you’re still having issues setting this up, you can send us a message via our support form so we can look into this further.
Hi all! Do you have a post or tutorial on how to configure this snippet with GP Unique ID to dynamically create a coupon code that is good for a future form submission? I’m just not seeing how this goes together….
Hi Ben,
You can get this done by setting the source_field_id parameter of the configuration to the ID of the Unique Field. If you’re still having issues setting this up, you can send us a message via our support form so we can look into this further.
Best,
Is it possible to use this to bulk generate codes? I have a list of 700 codes that I need to generate, could this be modified to do this? I really am hoping to avoid 1-by-1 manual code creation.
Any insight would be greatly appreciated. Thank you!
Hi Terry, you could combine this with GF Reload Form to speed up form submission.
How to connect discount to an affiliate integrate affiliatewp?
It’s likely possible but we don’t have a ready solution for this one. Let us know if you figure it out and we’ll update the article. 🙂
Hi David,
Is there anyway to display coupon usages left? Let’s say I have a coupon with coupon limit is 10. Users have used this coupon for 3 times. There are 7 times left. I want to display these information.
Thanks, Zak
Hi
thanks for the amazing snippet, really appreciated ;)
Would it be possible to create the coupons from more than 1 Form? I have 2 forms from wich I would like to create the coupon
Thanks for your help
Regards
Yup, you sure can. :)
More details: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Hi all,
Is there any way to display discount amount in let’s say Confirmation screen? Currently, I can display it but only with {pricing_fields} merge tag. Is they anyway to display Discount Amount specifically?
Thanks.
Hi Zak, the GP Subtotal & Tax plugin (available by request for Gravity Perks users) provides support for this via the {coupons} merge tag.
David,
This is great! I have Gravity Perks license. I will send support an email right away and see if I can have it.
Thanks, Zak
Replied. :)
Hi, Thanks for your code. For using Coupons on gravity form (and that Add on) i most have developer license. right?
Hi Ravid, yup, you’ll need a Gravity Forms Developer license to get access to the Coupons Add-on.
Thank you David. And for use that code i need the gf_after_submission hook. right?
And can I create an object without a variable, like on the examples?
Amazing.
Just wondering if it’s possible to have the user define an amount?
For example pass a price field value to the $amount variable?
Hi Leigh, this is possible but not without further customization. I took a look to see if it would be super simple to explain but it would take a number of steps to accomplish. I’d love to add support for this if you’re interested in commissioning the feature. Get in touch.
Hi David,
I figured out a quick fix, not the cleanest way but it works:
All I did was change
amount = $this->_args[‘amount’];
to
$amount = $entry[‘4’];
Where field 4 is a radio input or product option.
Thanks again for the great snippet.
[…] Monitor – With this tool you can perform simple and engaging email marketing campaigns to make spark in your business. This tool has simple template option that helps you in customizing […]
Hi
Thanks so much for this bit of code.
I have two questions: 1. I would like to know how to enable the coupon code to be valid on multiple specific forms:
‘meta’ => array( ‘form_id’ => 1,2,3, e.g.
) );
Thanks
Hi Tim, Gravity Forms does not support coupons enabled for multiple, specific forms. Only all forms or a single specific form.
You can set the expiration 6 months from the current date like so:
Hi, thanks for the snippit! I can imagine lots of applications!
Just wondering how it could be implemented to generate when they share the page?
Hi Guy, the simplest solution would be to have your share button submit a Gravity Form automatically (probably via AJAX).
Where do the coupons appear? I tried the code and changed the setting to gravity forms and woocommerce but I don’t see where the codes appear?
I want the coupon to be applied to the woocommerce shopping cart.
Hi David, if you’re trying to implement this for WooCommerce, I would check out this version of the snippet: https://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/
something is broken with the snippet. because if someone then goes to use a coupon generated by this snippet, it seems to not generate the proper ‘{pricing_fields}’ merge tag.
its missing the coupon discount
:/
this issue can be recreated anywhere….. try and use a coupon thatw as generated and make a notification with the merge tag {pricing_fields}
Hi Eric, I’m not able to recreate (success screenshot). Are you using the latest version of the GF Coupons add-on? Also, if you’ve installed this snippet previously, make sure you’re using the latest version of the snippet.
hmmm. I am using the latest of everythign and I still cant get it to work… the pricing field never shows the coupon and the deduction…
I have no idea what could have caused this problem…. I am using sandbox mode on paypal…. could that be a problem? Let me disable paypal and see if it works
i disabled my paypal feed and it works! but why?
also it would be nice if the coupon would only get made after good paypal submission….
(i see the snippet uses the hook after_post_submission… what if we added: add_action( ‘gform_paypal_post_ipn’, array( $this, ‘create_coupon…..)
so after extensive testing all day, I find that if there is a paypal feed enabled, the coupon that gets generated, if then used, seems to not properly create a {pricing_fields} tag.
Am I the only one with this problem?
Sorry. this was a gravity forms issue. But its being resolved in the next coupon update! all is well.
[…] Wiz put together another great snippet on creating coupons for the GF Coupon add-on that can be used on a subsequent […]
This snippet is much appreciated. I plan to use it to run a “buy one, get one free” promotion — if someone places an order, I will be providing them with a coupon code to place a second free order using the same order form in the future.