Skip to content

Instantly share code, notes, and snippets.

@max-kk
Last active April 15, 2017 09:59
Show Gist options
  • Save max-kk/e4cce72b786484a141b8803c15d7ff07 to your computer and use it in GitHub Desktop.
Save max-kk/e4cce72b786484a141b8803c15d7ff07 to your computer and use it in GitHub Desktop.
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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment