Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active October 7, 2021 15:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save spivurno/951d4ab966a26ab31465 to your computer and use it in GitHub Desktop.
Save spivurno/951d4ab966a26ab31465 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
<?php
/**
* WARNING! THIS SNIPPET MAY BE OUTDATED.
* The latest version of this snippet can be found in the Gravity Wiz Snippet Library:
* https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-create-coupon.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
* @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 {
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,
'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_code = rgar( $entry, $this->_args['source_field_id'] );
$amount = $this->_args['amount'];
$type = $this->_args['type'];
if( ! $coupon_code ) {
return;
}
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_code, $amount, $type, $entry, $form );
}
}
public function create_coupon_edd( $coupon_code, $amount, $type, $entry, $form ) {
if( ! is_callable( 'edd_store_discount' ) ) {
return;
}
$meta = wp_parse_args( $this->_args['meta'], array(
'name' => $coupon_code,
'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_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 {
GFCoupons::get_config( array( 'id' => 0 ), false );
}
$meta = wp_parse_args( $this->_args['meta'], array(
'form_id' => false,
'coupon_name' => $coupon_code,
'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 {
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_code, $amount, $type, $entry, $form ) {
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$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 ) {
update_post_meta( $new_coupon_id, $meta_key, $meta_value );
}
}
function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return $form_id == $this->_args['form_id'];
}
}
# Configuration
new GW_Create_Coupon( array(
// ID of the form which will be used to create coupons
'form_id' => 608,
// ID of the field whose value will be used as the coupon code
'source_field_id' => 1,
// which plugin the coupon should be created for (i.e. WooCommerce = 'wc')
'plugin' => '', // accepts: 'gf', 'wc', 'edd'
// type of coupon code to be created, available types will differ depending on the plugin
'type' => '',
// amount of the coupon discount
'amount' => 10
) );
<?php
/**
* Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
* http://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/
*/
/**
* 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'
) );
// 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'
) );
// 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'
)
) );
// Stackable 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'
)
) );
// All the things!
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'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' => '',
'usage_limit' => 1
)
) );
<?php
/**
* Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
* http://gravitywiz.com/creating-coupons-for-gravity-forms/
*/
/**
* GF Coupons
*/
// Coupon with Flat Discount, Applied to Total
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'flat'
) );
// Coupon with Percentage Discount, Applied to Total
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'plugin' => 'gf',
'amount' => 15,
'type' => 'percentage'
) );
// Stackable 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'
)
) );
// Unlimited Use Coupon during the month of December 2015 using Coupon Start and Coupon Expiration
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'
)
) );
// All the things!
new GW_Create_Coupon( array(
'form_id' => 608,
'source_field_id' => 1,
'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
)
) );
@nacm
Copy link

nacm commented Feb 19, 2018

Is there any way to create unique coupon codes. ie. check the existing coupons to make sure there is no duplicate.

@spivurno
Copy link
Author

spivurno commented Oct 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment