This page may contain affiliate links. Please read my disclaimer for more information.

How To Import Coupon Codes in Gravity Forms Coupons Add-On

  |  by Chris Eggleston

If you’re using Gravity Forms and the Coupons Add-On you may have one time or another needed to import coupon codes.

Unfortunately, this is not a built-in feature of Gravity Forms or the Coupons Add-On.

However, I was recently asked if this was possible and instead of saying no, decided to see if I could come up with a solution.

We have thousands of coupon codes. Is it possible to load these into Gravity Forms somehow?

In this tutorial, I’ll show you how to import hundreds or thousands of coupon codes into Gravity Forms Coupon Add-on, automatically!

If you want to skip this detailed tutorial, you can watch this video for a complete how to walk through:

Requirements

To complete this tutorial, you will need the following:

Because you will need to add a code snippet to your site, you will need to modify your themes functions.php file or use a functionality plugin. I recommend using a functionality plugin so I have listed a couple of options below:

Getting Setup

To start, we need to have all the pieces in place. Be sure that you have installed and activated Gravity Forms, the Coupons add-on, the GravityImport add-on, and one of the functionality plugins if you choose to use one.

Once that is done, we’ll start by adding the code snippet.

Adding the Code

Don’t let this step hold you back, the code might look scary but we only need to modify a couple of lines and I will show you exactly how to do that.

What this code does:

This snippet is designed to create a coupon code in the Coupons Add-On based on the value of a specified form field. It is executed after the form is submitted successfully.

Because the code is over 200 lines long, I have embedded it in the accordion below, but you can also grab the code by going to the Gravity Wiz site here.

Once you’ve got your copy of the code, go to the functionality plugin or your functions.php file and paste the code in.

This tutorial only demonstrates how to do this with the WPCodeBox plugin.

<?php
/**
 * Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
 *
 * Create coupons via Gravity Forms submissions. Map the coupon code to a field on the GF form and voila!
 *
 * @version 1.2.1
 * @author  David Smith <david@gravitywiz.com>
 * @license GPL-2.0+
 * @link    WooCommerce:   http://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/
 * @link    Gravity Forms: http://gravitywiz.com/creating-coupons-for-gf-coupons-add-on-with-gravity-forms/
 */
class GW_Create_Coupon {

	var $_args;

	public function __construct( $args = array() ) {

		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args( $args, array(
			'form_id'         => false,
			'source_field_id' => false,
			'name_field_id'   => false,
			'plugin'          => 'gf', // accepts: 'gf', 'wc', 'edd'
			'amount'          => 0,
			'type'            => '', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
			'meta'            => array(),
		) );

		// do version check in the init to make sure if GF is going to be loaded, it is already loaded
		add_action( 'init', array( $this, 'init' ) );

	}

	public function init() {

		// make sure we're running the required minimum version of Gravity Forms and that WooCommerce is active
		if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
			return;
		}

		add_action( 'gform_after_submission', array( $this, 'create_coupon' ), 10, 2 );

	}

	public function create_coupon( $entry, $form ) {

		if ( ! $this->is_applicable_form( $form ) ) {
			return;
		}

		$coupon_codes = $this->get_coupon_codes( $entry, $this->_args['source_field_id'] );

		foreach ( $coupon_codes as $coupon_code ) {

			if ( $this->_args['name_field_id'] === false ) {
				$coupon_name = $coupon_code;
			} else {
				$coupon_name = rgar( $entry, $this->_args['name_field_id'] );
				$coupon_name = $coupon_name === '' ? $coupon_code : $coupon_name;
			}

			$amount = $this->_args['amount'];
			$type   = $this->_args['type'];

			if ( is_callable( $amount ) ) {
				$amount = call_user_func( $amount );
			}

			$plugin_func = array( $this, sprintf( 'create_coupon_%s', $this->_args['plugin'] ) );

			if ( is_callable( $plugin_func ) ) {
				call_user_func( $plugin_func, $coupon_name, $coupon_code, $amount, $type, $entry, $form );
			}
		}

	}

	public function create_coupon_edd( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {

		if ( ! is_callable( 'edd_store_discount' ) ) {
			return;
		}

		$meta = wp_parse_args( $this->_args['meta'], array(
			'name'              => $coupon_name,
			'code'              => $coupon_code,
			'type'              => $type,
			'amount'            => $amount,
			'excluded_products' => array(),
			'expiration'        => '',
			'is_not_global'     => false,
			'is_single_use'     => false,
			'max_uses'          => '',
			'min_price'         => '',
			'product_condition' => '',
			'product_reqs'      => array(),
			'start'             => '',
			'uses'              => '',
		) );

		// EDD will set it's own defaults in the edd_store_discount() so let's filter out our own empty defaults (they're just here for easier reference)
		$meta = array_filter( $meta );

		// EDD takes a $details array which has some different keys than the meta, let's map the keys to the expected format
		$edd_post_keys = array(
			'max_uses'          => 'max',
			'product_reqs'      => 'products',
			'excluded_products' => 'excluded-products',
			'is_not_global'     => 'not_global',
			'is_single_use'     => 'use_once',
		);

		foreach ( $meta as $key => $value ) {
			$mod_key = rgar( $edd_post_keys, $key );
			if ( $mod_key ) {
				$meta[ $mod_key ] = $value;
			}
		}

		edd_store_discount( $meta );

	}

	public function create_coupon_gf( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {

		if ( ! class_exists( 'GFCoupons' ) ) {
			return;
		}

		// hack to load GF Coupons data.php file
		if ( is_callable( 'gf_coupons' ) ) {
			gf_coupons()->get_config( array( 'id' => 0 ), false );
		} else {
			/** @noinspection PhpDynamicAsStaticMethodCallInspection */
			GFCoupons::get_config( array( 'id' => 0 ), false );
		}

		$meta = wp_parse_args( $this->_args['meta'], array(
			'form_id'           => false,
			'coupon_name'       => $coupon_name,
			'coupon_code'       => strtoupper( $coupon_code ),
			'coupon_type'       => $type, // 'flat', 'percentage'
			'coupon_amount'     => $amount,
			'coupon_start'      => '', // MM/DD/YYYY
			'coupon_expiration' => '', // MM/DD/YYYY
			'coupon_limit'      => false,
			'coupon_stackable'  => false,
		) );

		$form_id = $meta['form_id'] ? $meta['form_id'] : 0;
		unset( $meta['form_id'] );

		foreach ( $meta as $key => $value ) {
			if ( $value instanceof Closure && is_callable( $value ) ) {
				$meta[ $key ] = call_user_func( $value, $entry, $form, $this );
			}
		}

		if ( is_callable( 'gf_coupons' ) ) {
			$meta['gravityForm']      = $form_id ? $form_id : 0;
			$meta['couponName']       = $meta['coupon_name'];
			$meta['couponCode']       = $meta['coupon_code'];
			$meta['couponAmountType'] = $meta['coupon_type'];
			$meta['couponAmount']     = $meta['coupon_amount'];
			$meta['startDate']        = $meta['coupon_start'];
			$meta['endDate']          = $meta['coupon_expiration'];
			$meta['usageLimit']       = $meta['coupon_limit'];
			$meta['isStackable']      = $meta['coupon_stackable'];
			$meta['usageCount']       = 0;
			gf_coupons()->insert_feed( $form_id, true, $meta );
		} else {
			/** @noinspection PhpUndefinedClassInspection */
			GFCouponsData::update_feed( 0, $form_id, true, $meta );
		}

	}

	/**
	 * Create a WooCommerce coupon.
	 *
	 * @link https://gist.github.com/mikejolley/3969579#file-gistfile1-txt
	 */
	public function create_coupon_wc( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {

		$start_date = rgar( $this->_args['meta'], 'start_date' );
		if ( $start_date === '' || ! strtotime( $start_date ) ) {
			$date       = current_datetime();
			$start_date = $date->format( 'Y-m-d H:i:s' );
		}

		// WooCommerce coupon uses the Post Title as the coupon code hence $coupon_code is assigned to Post Title and $coupon_name is assigned to the Post Content
		$coupon = array(
			'post_title'   => $coupon_code,
			'post_content' => $coupon_name,
			'post_status'  => 'publish',
			'post_author'  => 1,
			'post_type'    => 'shop_coupon',
			'post_date'    => $start_date,
		);

		$new_coupon_id = wp_insert_post( $coupon );

		$meta = wp_parse_args( $this->_args['meta'], array(
			'discount_type'              => $type,
			'coupon_amount'              => $amount,
			'individual_use'             => 'yes',
			'product_ids'                => '',
			'exclude_product_ids'        => '',
			'usage_limit'                => '1',
			'expiry_date'                => '',
			'apply_before_tax'           => 'no',
			'free_shipping'              => 'no',
			'exclude_sale_items'         => 'no',
			'product_categories'         => '',
			'exclude_product_categories' => '',
			'minimum_amount'             => '',
			'customer_email'             => '',
		) );

		foreach ( $meta as $meta_key => $meta_value ) {
			if ( $meta_value instanceof Closure && is_callable( $meta_value ) ) {
				$meta[ $meta_key ] = call_user_func( $meta_value, $entry, $form, $this );
			}
			update_post_meta( $new_coupon_id, $meta_key, $meta[ $meta_key ] );
		}

	}

	public function get_coupon_codes( $entry, $source_field_id ) {
		return array_filter( explode( "\n", rgar( $entry, $source_field_id ) ) );
	}

	function is_applicable_form( $form ) {

		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		return (int) $form_id === (int) $this->_args['form_id'];
	}

}

# Configuration

new GW_Create_Coupon( array(
	// ID of the form which will be used to create coupons
	'form_id'         => 176,
	// ID of the field whose value will be used as the coupon code
	'source_field_id' => 1,
	// ID of the field whose value will be used as the title of the coupon
	'name_field_id'   => 1,
	// which plugin the coupon should be created for (i.e. WooCommerce = 'wc')
	'plugin'          => 'gf', // accepts: 'gf', 'wc', 'edd'
	// type of coupon code to be created, available types will differ depending on the plugin
	'type'            => 'flat',
	// amount of the coupon discount
	'amount'          => 49,
	'meta'            => array(
		'form_id'           => false,
	//	'coupon_start'      => '', // MM/DD/YYYY
	//	'coupon_expiration' => '', // MM/DD/YYYY
		'coupon_limit'      => 1,
		'coupon_stackable'  => false
	)
) );

Building Your Form

We will need the form ID and a field ID to modify the code snippet, so before we can do that, let’s get our form built.

For this specific demo and use case, I used a form that has a single field labeled Coupon Code.

Field Type Used:

  • Single Line Text

If your use case requires additional fields, you can add as many fields as you’d like to this form. You can also use a field other than the Single Line Text field. For example, if your coupon codes are all numbers, you might choose to use a Numbers field instead.

Modifying the Code Snippet

Now we can make the modifications. The part of the code we’re going to change is at the end in the #Configuration section and it looks like this:

# Configuration

new GW_Create_Coupon( array(
	// ID of the form which will be used to create coupons
	'form_id'         => 176,
	// ID of the field whose value will be used as the coupon code
	'source_field_id' => 1,
	// ID of the field whose value will be used as the title of the coupon
	'name_field_id'   => 1,
	// which plugin the coupon should be created for (i.e. WooCommerce = 'wc')
	'plugin'          => 'gf', // accepts: 'gf', 'wc', 'edd'
	// type of coupon code to be created, available types will differ depending on the plugin
	'type'            => 'flat',
	// amount of the coupon discount
	'amount'          => 49,
	'meta'            => array(
		'form_id'           => false,
	//	'coupon_start'      => '', // MM/DD/YYYY
	//	'coupon_expiration' => '', // MM/DD/YYYY
		'coupon_limit'      => 1,
		'coupon_stackable'  => false
	)
) );

This video will walk you through how I modified the code for a specific use case, but if you need additional details on how to modify the code, you can use the comments or go to the Gravity Wiz tutorial where they have this code well documented.

Importing The Coupon Codes

Now that the form has been created, the code installed and modified you’re ready for the final step and to see the magic.

Before we go any further, you need to have your coupon codes and any other data you want to import in a CSV file.

Important note, make sure that your CSV has a header row, otherwise, the coupon in the first row will not be imported because the GravityImport plugin will assume it is the column header.

The Steps

  1. Go to Forms > Import/Export
  2. Click on Import Entries
  3. Drop your CSV file into the upload area
  4. Select An Existing Form (we created our form already)
  5. Select your form from the list
  6. Map the columns in the CSV file with your form fields
  7. Leave the default Configuration settings
  8. Click Continue with Import
  9. Check the results: go to Forms > Coupons

The Wrap Up

There you have it, a simple solution to import hundreds or thousands of coupon codes into the Gravity Forms Coupons Add-On.

Props to Gravity Wiz and GravityKit for providing the two independent solutions that make this possible.

If this saves you time and helps solve a major problem let us know we’d love to hear it.

As always if you have questions or need help, use the comments and we’ll be happy to help.

Photo of author
About the Author
Chris Eggleston
Chris is not just a Gravity Forms enthusiast; he's a dedicated father and loving husband. As the proud owner of WP Mantis, he's on a mission to simplify the WordPress experience for site owners. He brings a unique perspective to the Gravity Forms community.

Advertisement

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments