Create a Gravity Forms form which, when successfully submitted, will allow a user to redeem an email-restricted WooCommerce coupon.

The following is a code sample which can be used in conjunction with WooCommerce and Gravity Forms plugins for WordPress.

Complete Code

The complete code snippet for developers who are already comfortable with PHP, WordPress and Gravity Forms. Add this code snippet to the bottom of your theme’s function.php, but before any closing “?>” PHP tag.

// Adds action to Gravity Form form with the ID of 9
add_action("gform_after_submission_9", "h32b_add_user_to_promo_coupon_users", 10, 2);
//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Code Snippet Walk-through

Let’s review the first part of this code snippet:

The hook, add_action, is used in WordPress, and most plugins, to customize and extend functionality. In this example we’re using add action to extend functionality after Form ID #9 has been submitted successfully, using gform_after_submission_9, a Gravity Forms hook with the format of gform_after_submission_{form_id}.

Gravity Forms Documentation – “gform_after_submission”

// Adds action to Gravity Form form with the ID of 9
add_action("gform_after_submission_9", "h32b_add_user_to_promo_coupon_users", 10, 2);

For the next portion of code, we’ll need to get a few values:

  • Form ID
  • Email field ID
  • WooCommerce coupon code ID

Gravity Forms – Form ID

If you haven’t already created your Gravity Forms form, you will need to do so before proceeding with this tutorial. If you are unsure about how to create a form using Gravity Forms, please review the quick tutorial.

H32B - Gravity Forms and WooCommerce Coupon Tutorial - Form ID

For the example, our form ID is 9. This is the value we used in gform_after_submission_9.

Gravity Forms – Email field ID

Find the field ID of the email field used in your selected Gravity Form. You will find this value when you have selected to Edit the form, and you mouseover the Email field.

H32B - Gravity Forms and WooCommerce Coupon Tutorial - Email Field ID

For the example, our email field ID is 2. This is why we use $_POST[“input_2”] to retrieve the field’s value. If the email field ID was 26, we’d use $_POST[“input_26”]

WooCommerce – Coupon code ID

You will find the ID of your WooCommerce coupon code by visiting the WooCommerce > Coupons page, and clicking Edit under your specific WooCommerce coupon. The coupon code ID, is clearly displayed in the URL for the coupon’s edit screen: e.g. https://yourdispensary.com/wp-admin/post.php?post=13541&action=edit.

H32B - Gravity Forms and WooCommerce Coupon Tutorial - Coupon ID

For the example, our coupon code ID is 13541.

Custom Function

We may now examine our custom function, h32b_add_user_to_promo_coupon_users(), which fires after the form has been successfully submitted:

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Now at a glance this may seem a bit daunting, but we’ll simplify by looking at each line of code.

First we’re setting the email field ID and checking that the value is an email using the WordPress Data Validation function is_email(), $email_input = is_email($_POST[“input_2”]);. If the field is not an email address format, is_email() will return false.

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Next we’re setting the WooCommerce coupon code ID $coupon_id = 13541;, so we can retrieve the existing User Restrictions list of emails, and add our new email address.

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

If the email is not a valid format, if is_email() returns false, then nothing happens.

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

To get the current list of email addresses approved to the coupon code, get_post_meta($coupon_id, ‘customer_email’, true); we use the WordPress function: get_post_meta() with the meta key of “customer_email”.

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Now we can check the submission email against the existing emails in the WooCommerce coupon, using the PHP function: in_array(). Alternatively, we can also select the “No Duplicates” option for the email field in our Gravity Form.

The expression !in_array($email_input, $customer_email_array, true) means if the value of $email_input IS NOT in the array $customer_email_array the function will proceed. If the email already exists in the WooCommerce coupon’s customer_email array nothing happens.

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

We can now add the new email address, $email_input, to the end of the array of existing email addresses, $customer_email_array, using the PHP function: array_push()

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Finally, we can now add our updated array of customer emails, $customer_email_array, to our WooCommerce coupon code with the WordPress function: update_post_meta().

//Function to add user email to designated WooCommerce coupon code
function h32b_add_user_to_promo_coupon_users($form){
$email_input = is_email($_POST["input_2"]);
$coupon_id = 13541;
if ( $email_input ) {
$customer_email_array = get_post_meta($coupon_id, 'customer_email', true);
if ( !in_array($email_input, $customer_email_array, true) ) {
array_push($customer_email_array, $email_input);
update_post_meta($coupon_id, 'customer_email', $customer_email_array);
} }
} // h32b_add_user_to_promo_coupon_users

Review & Usage

Specifically for websites using WooCommerce and Gravity Forms plugins for WordPress, this code snippet makes use of several PHP and WordPress functions:

Leave a Reply

Your email address will not be published.