/**
 * Saves an edited discount
 *
 * @since 1.0
 * @param array $data Discount code data
 * @return void
 */
function edd_edit_discount($data)
{
    if (isset($data['edd-discount-nonce']) && wp_verify_nonce($data['edd-discount-nonce'], 'edd_discount_nonce')) {
        // Setup the discount code details
        $discount = array();
        foreach ($data as $key => $value) {
            if ($key != 'edd-discount-nonce' && $key != 'edd-action' && $key != 'discount-id' && $key != 'edd-redirect') {
                if (is_string($value) || is_int($value)) {
                    $discount[$key] = strip_tags(addslashes($value));
                } elseif (is_array($value)) {
                    $discount[$key] = array_map('absint', $value);
                }
            }
        }
        $old_discount = edd_get_discount_by('code', $data['code']);
        $discount['uses'] = edd_get_discount_uses($old_discount->ID);
        if (edd_store_discount($discount, $data['discount-id'])) {
            wp_redirect(add_query_arg('edd-message', 'discount_updated', $data['edd-redirect']));
            edd_die();
        } else {
            wp_redirect(add_query_arg('edd-message', 'discount_update_failed', $data['edd-redirect']));
            edd_die();
        }
    }
}
 public function generate_discount($email = '')
 {
     // Generate a 15 character code
     $code = substr(md5($email), 0, 15);
     if (edd_get_discount_by_code($code)) {
         return;
         // Discount already created
     }
     $details = array('name' => $email, 'code' => $code, 'max' => 1, 'amount' => '10', 'start' => '-1 day', 'expiration' => '+3 days', 'type' => 'percent', 'use_once' => true);
     $discount_id = edd_store_discount($details);
     return $code;
 }
/**
 * Saves an edited discount
 *
 * @since 1.0
 * @param array $data Discount code data
 * @return void
 */
function edd_edit_discount($data)
{
    if (!isset($data['edd-discount-nonce']) || !wp_verify_nonce($data['edd-discount-nonce'], 'edd_discount_nonce')) {
        return;
    }
    if (!current_user_can('manage_shop_discounts')) {
        wp_die(__('You do not have permission to edit discount codes', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    // Setup the discount code details
    $discount = array();
    foreach ($data as $key => $value) {
        if ($key != 'edd-discount-nonce' && $key != 'edd-action' && $key != 'discount-id' && $key != 'edd-redirect') {
            if (is_string($value) || is_int($value)) {
                $discount[$key] = strip_tags(addslashes($value));
            } elseif (is_array($value)) {
                $discount[$key] = array_map('absint', $value);
            }
        }
    }
    $old_discount = edd_get_discount_by('code', $data['code']);
    $discount['uses'] = edd_get_discount_uses($old_discount->ID);
    if (edd_store_discount($discount, $data['discount-id'])) {
        wp_redirect(add_query_arg('edd-message', 'discount_updated', $data['edd-redirect']));
        edd_die();
    } else {
        wp_redirect(add_query_arg('edd-message', 'discount_update_failed', $data['edd-redirect']));
        edd_die();
    }
}
    // WC Coupon Object
    $code = $c->post_title;
    $status = $c->post_status == 'publish' ? 'active' : 'inactive';
    $coupon = new WC_Coupon($code);
    $temp_log_str = "\nCoupon - {$c->ID}\n";
    $log_str .= $temp_log_str;
    echo $temp_log_str;
    $data = array('post_content' => $c->post_content, 'post_title' => $c->post_title, 'post_status' => $status, 'post_type' => $edd_coupon_cpt, 'post_author' => $c->post_author, 'post_parent' => $c->post_parent, 'post_excerpt' => $c->post_excerpt, 'post_date' => $c->post_date, 'post_date_gmt' => $c->post_date_gmt, 'comment_status' => $c->comment_status);
    $edd_coupon_id = wp_insert_post($data);
    // Adjust according to EDD Format
    $expiry_date = get_post_meta($c->ID, 'expiry_date', true);
    $expiry_date = new DateTime($expiry_date);
    $expiry_date->add(new DateInterval('PT23H59M59S'));
    $discount_type = get_post_meta($c->ID, 'discount_type', true);
    $data = array('name' => $c->post_excerpt, 'status' => $status, 'code' => $code, 'max' => get_post_meta($c->ID, 'usage_limit', true), 'amount' => get_post_meta($c->ID, 'coupon_amount', true), 'expiration' => $expiry_date->format('m/d/Y H:i:s'), 'type' => strstr($discount_type, 'percent') == FALSE ? 'flat' : 'percent', 'min_price' => get_post_meta($c->ID, 'minimum_amount', true), 'products' => array_map('intval', explode(',', get_post_meta($c->ID, 'product_ids', true))), 'product_condition' => 'any', 'excluded-products' => array_map('intval', explode(',', get_post_meta($c->ID, 'exclude_product_ids', true))), 'not_global' => true, 'use_once' => false);
    edd_store_discount($data, $edd_coupon_id);
    $wc_edd_coupon_map[$c->ID] = $edd_coupon_id;
    $temp_log_str = "\nWC Coupon migrated ...\n";
    $log_str .= $temp_log_str;
    echo $temp_log_str;
    update_post_meta($edd_coupon_id, '_wc_coupon_id', $c->ID);
}
/**
 * Step 4
 * Orders Migrate
 */
$wc_order_cpt = 'shop_order';
$edd_order_cpt = 'edd_payment';
// Fetch WC Coupons
$args = array('post_type' => $wc_order_cpt, 'posts_per_page' => -1, 'post_status' => 'any', 'orderby' => 'date', 'order' => 'ASC');
$wc_order_list = get_posts($args);
 /**
  * Create the NOD discount.
  *
  * This function generates the EDD Discount for the NOD offer using the preferences
  * that admin has defined in settings for expiry etc.
  * 
  * @params	int		$purchase_id	Required: The EDD purchase ID
  *
  * @return	obj|bool		The discount (WP_Post) object if successful, false on failure.
  */
 public function generate_discount($purchase_id)
 {
     $code = $this->generate_code();
     // Make sure the code does not already exist, otherwise create another
     if (edd_get_discount_by('code', $code)) {
         $code = $this->generate_code();
     }
     // Create the meta for the discount code
     $details = apply_filters('nod_discount_args', array('code' => $code, 'name' => __('NOD Discount for purchase ID ' . $purchase_id), 'status' => 'active', 'uses' => '', 'max' => '1', 'amount' => EDD_NOD()->settings->rate, 'start' => date('m/d/Y H:i:s'), 'expiration' => date('m/d/Y', strtotime("+" . EDD_NOD()->settings->expires)), 'type' => EDD_NOD()->settings->type, 'min_price' => '', 'product' => array(), 'product_condition' => '', 'excluded_products' => array(), 'not_global' => false, 'use_once' => '1'), $code, $purchase_id);
     // Before adding the new NOD discount code
     do_action('nod_before_nod_discount_code', $details);
     // Create the discount code. Return if unsuccessful.
     if (!edd_store_discount($details)) {
         return;
     }
     $discount = edd_get_discount_by_code($code);
     // After adding the new NOD discount code
     do_action('nod_after_nod_discount_code', $discount, $details);
     return !empty($discount) ? $discount : false;
 }