Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Created November 18, 2022 21:25
Show Gist options
  • Save zeshanshani/08daee399ed2c499cbd62a69ed90f7d3 to your computer and use it in GitHub Desktop.
Save zeshanshani/08daee399ed2c499cbd62a69ed90f7d3 to your computer and use it in GitHub Desktop.
This function allows you to programatically create GravityForms coupon.
<?php
function zawp_create_coupon_codes() {
if ( isset( $_GET['create_coupons'] ) ) {
$coupons = array(
array(
'coupon_name' => 'Agnieszka K - OCC - SML client code 100€ OFF',
'coupon_code' => 'AgnieszkaK100',
'amount' => '100',
'type' => 'flat',
'entry' => '',
'form' >= 0,
),
);
foreach ( $coupons as $coupon ) {
zawp_create_coupon_gf(
$coupon['coupon_name'],
$coupon['coupon_code'],
$coupon['amount'],
$coupon['type'],
$coupon['entry'],
$coupon['form'],
1
);
echo 'created.... ' . $coupon['coupon_name'];
}
}
}
add_action( 'admin_init', 'zawp_create_coupon_codes' );
<?php
/**
* Create Coupon Codes
*
* This function allows you to programatically create GravityForms coupon.
*
* @param string $coupon_name
* @param string $coupon_code
* @param integer/string $amount
* @param string $type
* @param string/integer $entry
* @param integer $form
* @param integer $limit
* @return void
*/
function zawp_create_coupon_gf( $coupon_name, $coupon_code, $amount, $type, $entry, $form, $limit ) {
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 = array(
'form_id' => false,
'coupon_name' => $coupon_name,
'coupon_code' => $coupon_code,
'coupon_type' => $type, // 'flat', 'percentage'
'coupon_amount' => $amount,
'coupon_start' => '', // MM/DD/YYYY
'coupon_expiration' => '', // MM/DD/YYYY
'coupon_limit' => $limit,
'coupon_stackable' => false,
);
$form_id = $meta['form_id'] ? $meta['form_id'] : 0;
unset( $meta['form_id'] );
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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment