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.
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.
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 (
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_use Set to 'yes'
if the coupon cannot be used in conjunction with other coupons.product_ids A 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_ids A 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_limit Set how many times this coupon can be used before it is void. Default value is 1
. Set to''
for unlimited usage.expiry_date Specify the date when the coupon expires. Format: 'YYYY-MM-DD'
. Example:2014-09-30
start_date Specify the date when the coupon becomes active. Format: 'YYYY-MM-DD'
. Example:2014-09-30
apply_before_tax Set to 'yes'
if the coupon should be applied before calculating cart tax.free_shipping Set to 'yes'
if the coupon grants free shipping. The free shipping method must be enabled with the “must use coupon” setting.exclude_sale_items Set to 'yes'
if the coupon should not apply to items on sale.product_categories A 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_categories Product 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_amount Set the minimum subtotal needed to use the coupon. maximum_amount Set the maximum subtotal allowed when using the coupon. customer_email Specify 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_items Specify 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_user Specify 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!
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
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.
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.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
This snippet can be initialized as many times as you want to create as many coupons as you need. These coupons can all be generated from the same form submission as well. Some generic examples of this here:
https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Does it work the same way for any coupon service as i have luxury car service for concerts and i want to import its coupons in the same way in open cart..
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.
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?
Hey Annabelle, just replied to your ticket as well. Only the configuration section should be changed. 🙂
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!
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.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!
I think I found the relevant post! Sorry to post unnecessarily.
Hi Kirsten,
No worries at all. Glad you were able to find what you want.
Best,
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?
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,
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
Hi Nick,
This is currently not supported. If you have a Gravity Perks License, you can get in touch via our support form so we see how we can assist you with this.
Best,
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!
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,
Is it possible to add a start date for WooCommer coupon like the GF coupon?
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,
Is it possible for the expiry date to be dynamically set like +3 days rather than setting an exact set date like 23/03/2021
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,
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!
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. 😃
I have a few questions because this might be just what I’m looking for:
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?
Is it then possible once they sign up to automatically log them into the website and apply the coupon to their cart?
Thank you! :)
Hi Nicole,
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.The above code is not creating coupons. I pasted the code into function file and change configurtion settings at bottom. For Field id, I am using email field id
Gravity form version – 2.4.20 Builder – Oxygen Builder Page – http://moikofix.hakusana.com/uutiskirjeen-tilaus/ Code – https://snippi.com/s/n7j7x6t
Hello Jaswinder, thanks for writing in. I just tested this one out and sadly was unable to recreate this issue on my side. Could you please make sure the full snippet has been installed correctly by checking out our snippet troubleshooting found here. Thanks 😀
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?
Hello Santiago, This is correct, the source field ID will be sent on the notification email if using the all fields merge tag.
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?
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.
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?
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. 😀
Hello,
Instead of an amount in the code, is there a way to use a source field for the amount value?
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.
I thought I responded to you. Thankyou :)
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(); } } }
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.
noob question : where do i input the coupons list in WordPress admin panel ? Wit WooCommerce > Coupon Im-Ex > Import Coupons ? Thx