Easy Digital Downloads - generate new coupon via PHP
<?php
// http://edd.wp-a2z.org/oik_api/edd_store_discount/
// https://gist.github.com/renventura/9a645b904279862614a4
function create_coupon_edd3466( $args ) {
if( ! is_callable( 'edd_store_discount' ) ) {
return;
}
// edd_discount_exists( $CODE );
$meta = wp_parse_args( $args, 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 (their 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;
}
}
$coupon_ID = edd_store_discount( $meta );
}