Creating Coupons for WooCommerce with Gravity Forms

Create coupons for WooCommerce with your Gravity Forms submissions and minimal configuration.

July 17, 2022: Added support for callable functions as coupon meta parameters.

July 22, 2021: Fixed an issue where scheduled coupons used GMT instead of the Time Zone configured in WordPress.

October 27, 2020: Migrated snippet to the Snippet Library.

Offering a coupon is a great way to incentivize users to fill out any form.

Want to provide a compelling incentive for visitors to sign up for your newsletter? Offer them a coupon code for a small discount at your shop.

Trying to spread the word about your online store? Offer customers a “Share this Product with a Friend” that will send that friend a discount to be used on their first purchase.

Note: We also provide a solution that integrates Gravity Forms natively with WooCommerce. Learn more about Gravity Shop Product Configurator.

This snippet also supports creating coupons for Easy Digital Downloads and Gravity Forms. 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.
  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.

Usage Examples

WooCommerce Coupon with Flat Discount, Applied to Cart

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart'
) );

Creates a flat $10 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.


WooCommerce Coupon with Percentage Discount, Applied to Cart

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'percent'
) );

Creates a 10% discount that applies to the entire cart.


WooCommerce Coupon with Percentage Discount, Applied to Specific Product(s)

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'percent_product',
	'meta'            => array(
		'product_ids' => '123'
	)
) );

Creates a 10% discount that applies to only to product ID 123.


WooCommerce Coupon with Start Date

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'percent_product',
	'meta'            => array(
		'start_date' => '2020-12-20'
	)
) );

Creates a 10% discount that can be used from December 20, 2020.


Stackable WooCommerce Coupon with Usage Limit and Expiration Date

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart',
	'meta'            => array(
		'individual_use' => 'no',
		'usage_limit'    => 5,
		'expiry_date'    => '12/31/2014'
	)
) );

Creates a flat $10 discount that applies to the entire cart. This coupon is can be used with other coupons (we set individual_use to 'no'). The coupon can be used up to 5 times (handled by the usage_limit) and will expire on December 31, 2014 (via the expiry_date).


WooCommerce Coupon with Name Set by Field Value

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'name_field_id'   => 20,
	'plugin'          => 'wc',
	'amount'          => 15,
	'type'            => 'fixed_cart', 
) );

Creates a flat $15 discount that applies to the entire cart. This coupon’s title is derived from the value in field ID 20 (handled by name_field_id).

WooCommerce Coupon with Product IDs Set by Field Value

new GW_Create_Coupon( array(
	'form_id'         => 608,
	'source_field_id' => 1,
	'plugin'          => 'wc',
	'type'            => 'fixed_product', 
	'amount'          => 10,
	'meta'            => array(
		'product_ids' => function() {
			return rgpost( 'input_2' );
		},
	),
) );

Creates a flat $10 discount that applies to the total of all products passed in the product_ids meta. The value of the product_ids meta is set dynamically based on the value submitted in field ID 2.


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'          => 'wc',
	'amount'          => 10,
	'type'            => 'fixed_cart', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
	'meta'            => array(
		'apply_before_tax'           => 'no',
		'customer_email'             => '',
		'exclude_product_categories' => array(),
		'exclude_product_ids'        => '',
		'exclude_sale_items'         => 'no',
		'expiry_date'                => '',
		'free_shipping'              => 'no',
		'individual_use'             => 'yes',
		'limit_usage_per_customer'   => '',
		'limit_usage_to_x_items'     => '',
		'maximum_amount'             => '',
		'minimum_amount'             => '',
		'product_categories'         => array(),
		'product_ids'                => '',
		'start_date'                 => '', // YYYY-MM-DD,
		'usage_limit'                => 1
	)
) );

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

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

    'fixed_cart'Applies a flat discount to the entire cart.
    'percent'Applies a percentage discount to the entire cart.
    'fixed_product'Applies a flat discount to a specific product(s).
    'percent_product'Applies a percentage discount to a specific product(s).
  • meta (array) (optional)

    An array of additional options that can be used to customize the generated coupon.

    individual_useSet to 'yes' if the coupon cannot be used in conjunction with other coupons.
    product_idsA comma-delimited list of products (by ID) which need to be in the cart to use this coupon or, for “Product Discounts”, which products are discounted.
    exclude_product_idsA comma-delimited list of products (by ID) which must not be in the cart to use this coupon or, for “Product Discounts”, which products are not discounted.
    usage_limitSet how many times this coupon can be used before it is void. Default value is 1. Set to '' for unlimited usage.
    expiry_dateSpecify the date when the coupon expires. Format: 'YYYY-MM-DD'. Example: 2014-09-30
    start_dateSpecify the date when the coupon becomes active. Format: 'YYYY-MM-DD'. Example: 2014-09-30
    apply_before_taxSet to 'yes' if the coupon should be applied before calculating cart tax.
    free_shippingSet to 'yes' if the coupon grants free shipping. The free shipping method must be enabled with the “must use coupon” setting.
    exclude_sale_itemsSet to 'yes' if the coupon should not apply to items on sale.
    product_categoriesA product must be in this category (use category ID) for the coupon to remain valid or, for “Product Discounts”, products in these categories will be discounted.
    exclude_product_categoriesProduct must not be in this category (use category ID) for the coupon to remain valid or, for “Product Discounts”, products in these categories will not be discounted.
    minimum_amountSet the minimum subtotal needed to use the coupon.
    maximum_amountSet the maximum subtotal allowed when using the coupon.
    customer_emailSpecify a list of emails to check against the customer’s billing email when an order is placed. Separate email addresses with commas.
    limit_usage_to_x_itemsSpecify the maximum number of individual items this coupon can apply to when using product discounts. Leave blank to apply to all qualifying items in cart.
    limit_usage_per_userSpecify how many times this coupon can be used by an invidual user. Uses billing email for guests, and user ID for logged in users.

* Parameter descriptions are modified versions of the default help tooltips available in the WooCommerce coupon edit page.

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. Ross
    Ross April 15, 2024 at 9:04 am

    Doesn’t work. It’s not creating woocommerce coupons. Here is my config class:

    new GW_Create_Coupon( array( // ID of the form which will be used to create coupons ‘form_id’ => 1, // ID of the field whose value will be used as the coupon code ‘source_field_id’ => 2, // ID of the field whose value will be used as the title of the coupon ‘name_field_id’ => 3, // which plugin the coupon should be created for (i.e. WooCommerce = ‘wc’) ‘plugin’ => ‘wc’, // accepts: ‘gf’, ‘wc’, ‘edd’ // type of coupon code to be created, available types will differ depending on the plugin ‘type’ => ‘percent’, // amount of the coupon discount ‘amount’ => 15, ‘meta’ => array( ‘individual_use’ => ‘yes’, ‘usage_limit’ => 1,

    ),
    // The IDs of the fields that must not be empty, else the coupon won't be created
    'required_fields' => array( 1, 2, 1.3 ),
    

    ) );

    Don’t know why is not working. I don’t have any errors.

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

      Hi Ross, can you test this? I closed the array() properly and added a closing parenthesis at the end. I also added a comma after ‘usage_limit’ => 1 to separate it from the next array item.

      If that doesn’t work and you have an active Gravity Perks license you can write us through support.

      new GW_Create_Coupon( array( // ID of the form which will be used to create coupons 'form_id' => 1, // ID of the field whose value will be used as the coupon code 'source_field_id' => 2, // ID of the field whose value will be used as the title of the coupon 'name_field_id' => 3, // which plugin the coupon should be created for (i.e. WooCommerce = 'wc') 'plugin' => 'wc', // accepts: 'gf', 'wc', 'edd' // type of coupon code to be created, available types will differ depending on the plugin 'type' => 'percent', // amount of the coupon discount 'amount' => 15, 'meta' => array( 'individual_use' => 'yes', 'usage_limit' => 1 ) ));

  2. Michael
    Michael April 5, 2023 at 11:06 am

    Dumb question. Where does this kick out the coupon to? Would love for this to be able to embed in an email notification that’s sent.

    Reply
    1. Scott Ryer
      Scott Ryer Staff April 5, 2023 at 2:41 pm

      Hi Michael,

      The field that is mapped to the source_field_id is the coupon code. You can include the merge tag for that field in your form notification to send it to the customer.

  3. Marta
    Marta March 7, 2023 at 8:26 pm

    Hi, can you modify this code to allow additional forms? So if I want more than one form used (form id-10 gives a 10% coupon, form id-11 gives 25%)

    Is this possible? Thanks

    Reply
    1. Scott Ryer
      Scott Ryer Staff February 13, 2023 at 10:49 am

      Hi Ellis,

      This snippet only supports WooCommerce. We also have snippets that can create coupons with Easy Digital Downloads and Gravity Forms Coupons. We don’t currently have a snippet that can make coupons for OpenCart.

  4. Annabelle Cloete
    Annabelle Cloete October 5, 2022 at 3:18 pm

    I am so sorry if this is a really dumb question, but the last step where you are saying configure the snippet, should these: [(form_id), (source_field_id), and the type and amount of the coupon] be change only in the last part of the snippet where it says ‘configuration’, or throughout the entire snippet?

    Reply
    1. David Smith
      David Smith Staff October 5, 2022 at 5:03 pm

      Hey Annabelle, just replied to your ticket as well. Only the configuration section should be changed. 🙂

  5. Jan
    Jan April 21, 2021 at 9:02 am

    We have installed an affiliate for Woocommerce plugin that add an ‘Assign to affiliate’-field to the coupons. We would like to extend this amazing snippet, so the coupon that was created through the form is automatically linked to the affiliate email address from the form. Is that something that is possible?

    Awesome life-saving snippet!

    Reply
    1. Scott Ryer
      Scott Ryer Staff April 21, 2021 at 10:54 am

      Hi Jan,

      Assuming the assignment is stored in the meta for the coupon, you should be able to do this using the meta parameter. If you are a Gravity Perks customer, drop us a line and we’ll be happy to dig into this to see if this is possible.

  6. Kirsten
    Kirsten March 19, 2021 at 7:24 am

    Hi! Do you have instructions for how to do this exact same thing, except to auto-generate a Gravity Forms Coupon Code (via the Coupons Add-On) instead of a WooCommerce coupon?

    Thanks!

    Reply
  7. Parandis
    Parandis March 17, 2021 at 8:10 pm

    Hi,

    I was looking for a solution for my issue and this was the closest one I found, I have some specific coupon codes from the client and they wanted to randomly send it to whoever submitted the form. is there any way around this code to make it happen? Like I add the coupon codes to woo-commerce and just use those specific ones for people who sign up instead of creating the codes?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff March 18, 2021 at 6:08 am

      Hi Parandis,

      You can manually enter those specific codes one after the other when creating the coupon code using this tutorial. You can also import all the codes into WooCommerce. You can read more about that here

      Best,

  8. Nick
    Nick February 15, 2021 at 9:07 pm

    Hi team,

    is it possible to schedule start & end dates for coupon based on calendar input date select on Gravity Form?

    I know I can schedule expiry date (+3 days) etc based on submit date, but, i want to take the coupon start date & end date based on inputted date select from form..

    Thanks! Nick

    Reply
  9. Ignacio
    Ignacio January 15, 2021 at 10:00 pm

    Hello, thanks for the snippet, very useful! I am trying to set up the product id based on a field value, copying the example you gave to another person above with regards to amount, but I’m getting a fatal error on form submission. This is that part of the code:

    ‘meta’ => array( ‘product_ids’ => function() { return rgpost( ‘input_2’ ); }, ‘start_date’ => ‘{today:+2 weeks}’, // YYYY-MM-DD )

    I guess it doesn’t work because the function is inside an array, so maybe it should be formatted differently? Any help is really appreciated. Thanks!

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff January 18, 2021 at 8:29 am

      Hi Ignacio,

      The snippet currently doesn’t support setting the Product ID using a field value. You’ll have to do some customization to the snippet to get it working for you this way. I’ll log this as a feature request for the snippet. If we receive more requests for this feature, we’ll look into adding it to the snippet.

      That said, if you’re an active Gravity Perk License holder, you can get in touch via our support contact form so we see how can assist you with this.

      Best,

    1. Samuel Bassah
      Samuel Bassah Staff November 25, 2020 at 2:42 pm

      Hi Edi,

      We’ve updated the snippet to allow users to set the start date for WooCommerce Coupons. The instruction on how to set it up is within the documentation above and you can get the latest version from the snippet library.

      Cheers,

    1. Samuel Bassah
      Samuel Bassah Staff October 13, 2020 at 12:30 pm

      Hi Nick,

      If you want to set the coupon expiration date to be +3 days after the coupon code is created, replacing the exact date with this Date(‘m/d/Y’, strtotime(‘+3 days’)) should work for you. So the parameter will look like this

      'expiry_date' => Date('m/d/Y', strtotime('+3 days')),

      Best,

  10. George
    George October 5, 2020 at 9:14 am

    Hey guys, can’t make it work myself. Using the latest Gravity Forms, WooCommerce, and Elementor. I am not getting any console errors or log errors on the server, although it doesn’t seem to be adding coupons on form submission. Any ideas? Thanks for the great work!

    Reply
    1. Ryan Donovan
      Ryan Donovan October 5, 2020 at 11:01 am

      Hello George, this is a bit of a strange one as I tested this in my own environment. Have you ran a theme/plugin conflict test to rule out what could possibly be causing the error? Let us know the results and we can try and help you best we can. 😃

  11. Nicole
    Nicole September 18, 2020 at 4:51 pm

    I have a few questions because this might be just what I’m looking for:

    1. I have a WooCommerce store and I would like to offer my customers a member discount. So I would like them to sign up using a gravity form, pay a fee (that’s recurring), and have a coupon code generated for that customer that can only be used with the email address that they signed up with during checkout for the coupon to work. Is all of this possible?

    2. Is it then possible once they sign up to automatically log them into the website and apply the coupon to their cart?

    Thank you! :)

    Reply
    1. Scott Ryer
      Scott Ryer Staff September 21, 2020 at 10:38 am

      Hi Nicole,

      1. All of that is possible except for automatically restricting the email address. The snippet does allow you add a list of emails using the customer_email meta key, however it cannot currently use a field’s value. If you’re a Gravity Perks customer, drop us a line and we can look into adding support for that in the snippet.
      2. You can use Auto Login to automatically log them in after registering. We don’t have a solution for automatically applying a coupon to the cart.
  12. Santiago
    Santiago April 29, 2020 at 9:24 am

    Hi there, this is a great idea. So the coupon code that is created automatically, can be also be sent in the form notification email? source_field_id?

    Reply
  13. PeterG
    PeterG April 24, 2020 at 2:57 am

    I am trying to get this to auto-apply the coupon code to the cart. I am struggling to see how to do that or if it is possible. I submit the form and it creates the coupon code just fine, but isnt applied to the cart. is there something i am doing wrong?

    Reply
    1. Ryan Donovan
      Ryan Donovan April 24, 2020 at 9:55 am

      Hello Peter, this article demonstrates how to create a WooCommerce coupon when a Gravity Form is submitted. Unfortunately, some custom code will be needed if you want to auto-apply a coupon that is being created after submission.

  14. Gary
    Gary March 23, 2020 at 7:23 pm

    Im not sure what would go in this field Is it a field that goes in the form? If so. What type of field and how would I implement it?

    Reply
    1. Ryan Donovan
      Ryan Donovan March 24, 2020 at 10:25 am

      Hello Gary, The basic configuration only requires that you specify which form should be used to generate coupons (form_id), which field’s value should be used as the coupon code (source_field_id), as well as the type and amount of the coupon. As for where this would go, you would Copy and paste the entire snippet into your theme’s functions.php file. If you need help installing the snippet, check out our help article here. 😀

    1. David Smith
      David Smith Staff January 7, 2020 at 3:36 pm

      Hi Jessica, the amount can be passed as a callable function. You could something like:

      'amount' => function() { return rgpost( 'input_1' ); },

      This would return whatever value was submitted for field ID 1 as the amount for the coupon.

  15. Alvina
    Alvina September 13, 2019 at 1:43 am

    Why my coupon is not working? I have gone through this tutorial https://www.cloudways.com/blog/create-woocommerce-coupon-code/ and implemented the same steps that mentioned in this tutorial but I am having an error when I add coupon it’s saying the coupon is not valid. Can you please tell me any alternative to do this? Here is my code

    add_action( ‘woocommerce_before_cart’, ‘sh_coupons_matched’ ); function sh_coupons_matched() { global $woocommerce; $sh_coupon = ‘OnlineShop’; if ( $woocommerce->cart->has_discount( $sh_coupon ) ) return; foreach ( $woocommerce->cart->cart_contents as $key => $values ) { $autocoupon = array( 65 ); if( in_array( $values[‘product_id’], $autocoupon ) ) { $woocommerce->cart->add_discount( $cw_coupon ); wc_print_notices(); } } }

    Reply
    1. David Smith
      David Smith Staff September 13, 2019 at 1:03 pm

      Hi Alvina, this article demonstrates how to create a WooCommerce coupon when a Gravity Form is submitted. The code from the other article does not directly relate to this snippet.

  16. Thessaly
    Thessaly August 9, 2018 at 3:27 pm

    noob question : where do i input the coupons list in WordPress admin panel ? Wit WooCommerce > Coupon Im-Ex > Import Coupons ? Thx

    Reply
  17. Maximilian
    Maximilian March 21, 2018 at 7:54 am

    I already have Gravity Forms for a year but I didn’t know you could create coupons for WoCommerce. Good to know. Thanks :)

    Reply
    1. Darren
      Darren January 24, 2018 at 1:45 pm

      Thanks David, works perfectly. One more question if I may, Where do I enter the Coupons description?

      Will make it easier to list only “Refer a Friend” coupons in the Admin.

  18. Katy
    Katy November 30, 2017 at 10:48 am

    Hi David, I’m wondering if there is a way to make the date field “today’s date + 1.” In other words, I want the coupon to expire in 24 hours. Is this possible?

    Reply
  19. Create Random Discount Codes for EDD with Gravity Forms February 12, 2017 at 4:38 pm

    […] There you have it. You now have a system for rewarding your customers with a discount code to your Easy Digital Downloads store after they submit a form. If you have any questions, feel free to drop a comment below and I’ll do my best to help you out. You can also leave a comment for David here. […]

    Reply
  20. Rochelle
    Rochelle November 21, 2016 at 9:17 pm

    I am wanting to only restrict the 10% off coupon to the people who filled in the gravity form. If I am in the woocommerce cart and entered the GF coupon code, the cart changes the pricing accordingly. I want check that the email address the customer enters into woocommerce is one that was submitted to the GF that the code was generated from. I tried adding ‘customer_email’ => function_exists( ‘rgpost’ ) ? rgpost( ‘input_1’ ) : ” to the list of parameters in the ” new GW_Create_Coupon( array( ” but this didn’t seem to work when I tested it as the order went through with the discount applied when I used a different email address than the one I used in GF.

    Reply
  21. Amin
    Amin June 15, 2016 at 7:03 pm

    Hi, and thank you for the code.

    I’m intending to use this snippet for a store with multiple vendors. So I suppose I need to change (‘post_author’ => 1) in line 168 with (current_user_id) or something like that ? Is it possible to let vendors through this form select the products ID by title (instead of inserting them by default in php) and pick the expiration date too by themselves ?

    Thank you in advance !

    Reply
    1. David Smith
      David Smith Staff June 30, 2016 at 9:10 am

      Hi Amin, it is possible but it would require custom work that we’re not able to provide at this time.

  22. Bastien
    Bastien February 17, 2016 at 4:34 pm

    Thank you for this code David.

    Before we purchase the Perks bundle, can you tell me if it’s possible to display the coupon code in the confirmation message (and in the notification email) after the user submits the form/survey? I’m not sure how I would achieve this feat.

    Thank you!

    Regards. B

    Reply
    1. David Smith
      David Smith Staff February 18, 2016 at 9:49 pm

      Since the coupon code is just a field on the form, you can use the field’s merge tag to output the coupon any place GF merge tags are supported (i.e. notifications and confirmations). :)

  23. Evie
    Evie January 7, 2016 at 9:58 pm

    Hello,

    Awesome job! Question, the issue I am running into is with my form I am using the product pricing field with the quantity. With that being said the “woocommerce” quantity doesn’t automatically update the GF quantity. This is affecting my coupons since woocommerce only see 1 item in the cart when there may be 5. My coupon in woocommerce is a product discount so it should apply to each product in the cart but it isn’t because it doesn’t recognize the GF quantity. I don’t get it because it updates the total properly. Will this code help me with this?

    Reply
    1. David Smith
      David Smith Staff March 15, 2016 at 6:16 am

      Hi Evie, no, this snippet just allows you to create a WooCommerce coupon based on a Gravity Forms submission. It’s more of a special incentive you can offer your users for submitting the form.

  24. Brandon Shutter
    Brandon Shutter December 17, 2015 at 2:57 pm

    Is there a way to do a random string for the coupon code? I’m planning on hundreds of people using this form (it’s a survey) and I want them to each have their own coupon code. I use Smart Coupons to generate coupons, but I don’t see a way to tie into that.

    Reply
    1. David Smith
      David Smith Staff December 17, 2015 at 11:26 pm

      Not sure how Smart Coupons works but if you’re already using GF, our GP Unique ID plugin integrates with this really well. Just add a Unique ID field to your form and set the field ID as the “source_field_id” parameter.

  25. Marieke
    Marieke October 16, 2015 at 6:27 am

    Love this code, although I can’t get it to work with multiple product ids, it creates a coupon code on submission but only adds one product to the code.

    Can you help me in the right direction, this is part of the code that has the ids in it:

    ‘product_ids’ => ‘103’,’111′,’112′,’113′,’114′,’115′,’116′,’90’,’92’,’93’

    It olny adds the first iD, the rest is ignored.

    Thank you

    Reply
    1. Phil Ingram
      Phil Ingram October 16, 2015 at 11:35 am

      Simple =)

      Your id’s all need to be wrapped inside the same set of quotes.

      ‘product_ids’ => ‘103,111,112,113,114,115,116,90,92,93’

    2. Marieke
      Marieke October 16, 2015 at 1:09 pm

      Hmmm.. now I feel slightly blond!! Good thing that I am!

      That I haven’t figured that one out?!? I am using another code to add a fee to certain product ids, and that code uses the ‘ ‘, ‘ ‘.. so I just figured it would be the same for this.

      Cheers for the solution, just tested and off course it works!

  26. Keenan
    Keenan July 10, 2015 at 4:33 pm

    Hi, this snippet looks great. But when I drop it into my functions.php file, I get this error: “Class ‘GW_Create_Coupon’ not found in /path/to/my/functions.php”

    You can see here: http://projects.kemdev.com/

    I just copied and pasted it directly from your example above. Any thoughts? Thanks!

    Reply
    1. Keenan
      Keenan July 10, 2015 at 4:51 pm

      Nevermind. I figured it out. Didn’t realize there was a whole slew of code I needed to add, not just the tiny little configuration snippet. Must get more coffee!

  27. Richie Tyler
    Richie Tyler December 16, 2014 at 8:38 pm

    Hi David, first up thanks for your phenomenal and consistently helpful advice and snippets. Just wondering when the ‘Creating coupons for GF Coupons Add-on’ article might go live?

    Reply
  28. Neil
    Neil October 14, 2014 at 2:31 am

    Can’t seem to find any way to get in touch with a pre-purchase question on your Perks package. I need to know if the Conditional Pricing plugin will work in conjunction with the Gravity Forms Product Add-Ons plugin and then allow Calculations using Merge Tags in order to apply a percentage based increase or decrease in total price based on a final selection in the form. Hope I’m making sense, and that this will work :) Please let me know, thanks!

    Reply
    1. David Smith
      David Smith Staff October 14, 2014 at 6:15 pm

      Hi Neil, GP Conditional Pricing will allow you to create set prices per product depending on values entered on the field. It does not currently support percentage-based pricing but the price of a non-Calculation product field can be used in a Calculation field to create a modified price. If you’d like to send me a specific example of your pricing structure, I can confirm:

      https://gravitywiz.com/have-a-question-about-gravity-perks/

  29. Phil Ingram
    Phil Ingram October 12, 2014 at 1:22 am

    This is awesome, thanks for the snippet. This works phenomenally with woocommerce but my client has recently decided to use an off-site offering for his ebook called revizzit so now I have to import those vouchers into my wordpress db and release them one per successful purchase. Still haven’t decided the best route to go yet…CPT or private table…but this gives me inspiration!

    Reply
  30. Create Random Discount Codes for EDD with Gravity Forms October 3, 2014 at 3:05 pm

    […] is made possible by yet another great Gravity Forms snippet from David Smith at GravityWiz.com, Creating Coupons for WooCommerce with Gravity Forms. This snippet integrates with WooCommerce, Easy Digital Downloads and Gravity Forms Coupons. […]

    Reply
  31. Weekly Roundup: October 3, 2014 – Sell with WP October 3, 2014 at 2:00 am

    […] a great tutorial on creating WooCommerce coupons via Gravity Forms from Gravity […]

    Reply
  32. Shawn
    Shawn October 1, 2014 at 2:23 pm

    OK… Can we send this to multiple email addresses? So I send it to 10 email addresses to restrict the use of the 10 coupon codes. I also want to use this on ONE product…

    Reply
    1. David Smith
      David Smith Staff October 1, 2014 at 6:10 pm

      Sure thing, Shawn. Here is an example of the parameters for such a configuration. Keep in mind, you’ll still need to update values to meet your exact needs.

      http://pastie.org/9611392

      In the meta parameter, we set the customer_email to comma-delimited list of emails to users who should be able to use this coupon. We set the limit_usage_per_customer to 1 so each user can only use it once. Lastly, we set the products_ids parameter to the product ID to which this coupon should apply.

      Since the coupon code itself will always be pulled from a Gravity Form field, you can use GF’s notifications to send the coupon (use the field’s merge tag to include it in the message) to the users who should receive it.

  33. Ren
    Ren September 30, 2014 at 10:09 am

    Hey David,

    Another great snippet! I was actually just thinking about this exact thing the other day but for Easy Digital Downloads. It’d be great to see this snippet extended to EDD as well.

    Excellent work as always.

    Ren

    Reply
    1. David Smith
      David Smith Staff September 30, 2014 at 5:48 pm

      Hey Ren, glad you like it. It actually already supports EDD and will detail the available parameters in an upcoming article. For now, here’s a quick example of how you can instantiate this plugin for EDD right now:

      http://pastie.org/9608616#6

    2. Ren
      Ren September 30, 2014 at 6:39 pm

      Ah, I see that. I overlooked that info earlier. Thanks for snippet on instantiating for EDD. I look forward to the EDD specific post.

  34. Farrel
    Farrel September 30, 2014 at 12:30 am

    Thanks for the code. Just one question. Can we restrict the coupon use to the user who was registering with the form? If so what is the correct way to set the coupon recipient ID to be the same user ID as the person whose user account is being created?

    Thanks

    Reply
    1. David Smith
      David Smith Staff September 30, 2014 at 5:54 pm

      Hi Farrel, you can restrict the coupon to a static email by setting the “customer_email” parameter to that static email. In your case, since you want to use the email that the user submitted with the form, you could try setting the “customer_email” parameter like so:

      'customer_email' => function_exists( 'rgpost' ) ? rgpost( 'input_1' ) : ''

      Replace the “1” in “input_1” with the ID of your email field. This will check the submitted content for that email and populate it as the value of the parameter.

    2. Sam Mitzmann
      Sam Mitzmann August 17, 2017 at 2:24 am

      I tried adding that parmaeter but I got this error: syntax error, unexpected ”customer_email” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’

    3. Sam Mitzmann
      Sam Mitzmann August 17, 2017 at 2:38 am

      The error was caused by a missing comma :) However, the restrict email to the email submitted in form is not applying to the coupon that is created and the ‘individual_use’ => ‘no’, is still generating a coupon that is not stackable.

    4. Tevya
      Tevya March 16, 2018 at 7:33 pm

      Wanted to see if you got this sorted for Sam? I’d like to do the same thing: generate a coupon that’s locked to the submitter’s email. I assume the easiest is just to add the submitted email to the “Email restrictions” field of the coupon. But I’m not sure how to do that?

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 WooCommerce with Gravity Forms

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