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.

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. Keep an eye out for these awesome articles coming soon:

Getting Started

  1. Check requirements

  2. Install the snippet

    • Copy and paste the entire snippet into your theme’s functions.php file. Having trouble installing the snippet? Read this.
  3. 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 the type and amount of the coupon.
    • See the Usage Examples and available Parameters below.

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'         => 608,
	'source_field_id' => 1,
	'name_field_id'   => 20,
	'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
	)
) );

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

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!

Comments

  1. Kahl
    Kahl February 13, 2024 at 4:15 am

    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.

    Reply
    1. Dario
      Dario Staff February 13, 2024 at 12:15 pm

      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,

  2. Sahar
    Sahar February 17, 2023 at 8:39 pm

    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)

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 20, 2023 at 6:10 am

      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,

  3. annabelle
    annabelle October 4, 2022 at 3:38 am

    Do you only need gravity forms purchased or do you also need gravity perks?

    As it is not working with gravity forms alone.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff October 4, 2022 at 5:32 am

      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,

  4. JoSeO
    JoSeO August 23, 2022 at 1:04 pm

    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.

    Reply
    1. Dario Space
      Dario Space Staff August 23, 2022 at 1:13 pm

      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,

  5. sharlene chan
    sharlene chan January 14, 2021 at 4:50 pm

    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.

    Reply
  6. Mike
    Mike January 14, 2021 at 4:47 pm

    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’ ) ?

    Reply
    1. Ryan Donovan
      Ryan Donovan January 15, 2021 at 10:55 am

      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.

  7. Bet Hannon
    Bet Hannon November 18, 2020 at 5:26 pm

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

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff November 18, 2020 at 11:17 pm

      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,

  8. Terry Majamaki
    Terry Majamaki February 5, 2020 at 9:02 am

    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!

    Reply
    1. David Smith
      David Smith Staff December 17, 2019 at 11:44 am

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

  9. Zak
    Zak November 30, 2017 at 9:36 pm

    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

    Reply
  10. Luca
    Luca April 4, 2017 at 4:04 pm

    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

    Reply
  11. Zak
    Zak January 19, 2017 at 3:15 am

    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.

    Reply
    1. Hieu Hun
      Hieu Hun January 19, 2017 at 8:52 am

      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

  12. Leigh
    Leigh November 9, 2016 at 10:55 pm

    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?

    Reply
    1. David Smith
      David Smith Staff November 10, 2016 at 3:53 pm

      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.

    2. Leigh
      Leigh November 10, 2016 at 8:09 pm

      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.

  13. Gravity Forms 25% Coupon & $50 Discount Code - Web@.net May 14, 2016 at 10:13 am

    […] 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 […]

    Reply
  14. tim
    tim December 2, 2015 at 6:49 am

    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.

    )
    

    ) );

    1. How can I have a expiration date that is 6 months from when the coupon code is generated?

    Thanks

    Reply
    1. David Smith
      David Smith Staff December 5, 2015 at 8:19 am

      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:

      'meta' => array( 'expiration_date' => date( 'm/d/Y', strtotime( '+6 months' ) ) )

  15. Guy
    Guy November 11, 2015 at 2:10 am

    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?

    Reply
    1. David Smith
      David Smith Staff November 22, 2015 at 6:04 pm

      Hi Guy, the simplest solution would be to have your share button submit a Gravity Form automatically (probably via AJAX).

  16. David
    David September 3, 2015 at 5:44 pm

    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.

    Reply
  17. eric
    eric July 22, 2015 at 2:53 pm

    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}

    Reply
    1. David Smith
      David Smith Staff July 23, 2015 at 9:03 am

      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.

    2. Eric
      Eric July 23, 2015 at 9:27 am

      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

    3. Eric
      Eric July 23, 2015 at 9:31 am

      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…..)

    4. Eric
      Eric July 23, 2015 at 9:52 pm

      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?

    5. Eric
      Eric July 27, 2015 at 2:47 pm

      Sorry. this was a gravity forms issue. But its being resolved in the next coupon update! all is well.

  18. Weekly Roundup: January 16, 2015 – Sell with WP January 16, 2015 at 3:00 am

    […] Wiz put together another great snippet on creating coupons for the GF Coupon add-on that can be used on a subsequent […]

    Reply
  19. Erik
    Erik January 12, 2015 at 4:15 pm

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Creating Coupons for GF Coupons Add-on with Gravity Forms

This field is for validation purposes and should be left unchanged.