/** * Stores a discount code. If the code already exists, it updates it, otherwise * it creates a new one. * * @since 1.0 * @param string $details * @param int $discount_id * @return bool Whether or not discount code was created */ function edd_store_discount($details, $discount_id = null) { $meta = array('code' => isset($details['code']) ? $details['code'] : '', 'name' => isset($details['name']) ? $details['name'] : '', 'status' => isset($details['status']) ? $details['status'] : 'active', 'uses' => isset($details['uses']) ? $details['uses'] : '', 'max_uses' => isset($details['max']) ? $details['max'] : '', 'amount' => isset($details['amount']) ? $details['amount'] : '', 'start' => isset($details['start']) ? $details['start'] : '', 'expiration' => isset($details['expiration']) ? $details['expiration'] : '', 'type' => isset($details['type']) ? $details['type'] : '', 'min_price' => isset($details['min_price']) ? $details['min_price'] : '', 'product_reqs' => isset($details['products']) ? $details['products'] : array(), 'product_condition' => isset($details['product_condition']) ? $details['product_condition'] : '', 'excluded_products' => isset($details['excluded-products']) ? $details['excluded-products'] : array(), 'is_not_global' => isset($details['not_global']) ? $details['not_global'] : false, 'is_single_use' => isset($details['use_once']) ? $details['use_once'] : false); $start_timestamp = strtotime($meta['start']); if (!empty($meta['start'])) { $meta['start'] = date('m/d/Y H:i:s', $start_timestamp); } if (!empty($meta['expiration'])) { $meta['expiration'] = date('m/d/Y H:i:s', strtotime(date('m/d/Y', strtotime($meta['expiration'])) . ' 23:59:59')); $end_timestamp = strtotime($meta['expiration']); if (!empty($meta['start']) && $start_timestamp > $end_timestamp) { // Set the expiration date to the start date if start is later than expiration $meta['expiration'] = $meta['start']; } } if (!empty($meta['product_reqs'])) { foreach ($meta['product_reqs'] as $key => $product) { if (0 === intval($product)) { unset($meta['product_reqs'][$key]); } } } if (!empty($meta['excluded_products'])) { foreach ($meta['excluded_products'] as $key => $product) { if (0 === intval($product)) { unset($meta['excluded_products'][$key]); } } } if (!empty($discount_id) && edd_discount_exists($discount_id)) { // Update an existing discount $meta = apply_filters('edd_update_discount', $meta, $discount_id); do_action('edd_pre_update_discount', $meta, $discount_id); wp_update_post(array('ID' => $discount_id, 'post_title' => $meta['name'], 'post_status' => $meta['status'])); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_update_discount', $meta, $discount_id); // Discount code updated return $discount_id; } else { // Add the discount $meta = apply_filters('edd_insert_discount', $meta); do_action('edd_pre_insert_discount', $meta); $discount_id = wp_insert_post(array('post_type' => 'edd_discount', 'post_title' => $meta['name'], 'post_status' => 'active')); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_insert_discount', $meta, $discount_id); // Discount code created return $discount_id; } }
/** * Stores a discount code. If the code already exists, it updates it, otherwise * it creates a new one. * * @since 1.0 * @param string $details * @param int $discount_id * @return int The discount ID of the discount code, or WP_Error on failure. */ function edd_store_discount($details, $discount_id = null) { $meta = array('code' => isset($details['code']) ? $details['code'] : '', 'name' => isset($details['name']) ? $details['name'] : '', 'status' => isset($details['status']) ? $details['status'] : 'active', 'uses' => isset($details['uses']) ? $details['uses'] : '', 'max_uses' => isset($details['max']) ? $details['max'] : '', 'amount' => isset($details['amount']) ? $details['amount'] : '', 'start' => isset($details['start']) ? $details['start'] : '', 'expiration' => isset($details['expiration']) ? $details['expiration'] : '', 'type' => isset($details['type']) ? $details['type'] : '', 'min_price' => isset($details['min_price']) ? $details['min_price'] : '', 'product_reqs' => isset($details['products']) ? $details['products'] : array(), 'product_condition' => isset($details['product_condition']) ? $details['product_condition'] : '', 'excluded_products' => isset($details['excluded-products']) ? $details['excluded-products'] : array(), 'is_not_global' => isset($details['not_global']) ? $details['not_global'] : false, 'is_single_use' => isset($details['use_once']) ? $details['use_once'] : false); $start_timestamp = strtotime($meta['start']); if (!empty($meta['start'])) { $meta['start'] = date('m/d/Y H:i:s', $start_timestamp); } if (!empty($meta['expiration'])) { $meta['expiration'] = date('m/d/Y H:i:s', strtotime(date('m/d/Y', strtotime($meta['expiration'])) . ' 23:59:59')); $end_timestamp = strtotime($meta['expiration']); if (!empty($meta['start']) && $start_timestamp > $end_timestamp) { // Set the expiration date to the start date if start is later than expiration $meta['expiration'] = $meta['start']; } } if (!empty($meta['excluded_products'])) { foreach ($meta['excluded_products'] as $key => $product) { if (0 === intval($product)) { unset($meta['excluded_products'][$key]); } } } if (!empty($discount_id) && edd_discount_exists($discount_id)) { // Update an existing discount $meta = apply_filters('edd_update_discount', $meta, $discount_id); do_action('edd_pre_update_discount', $meta, $discount_id); wp_update_post(array('ID' => $discount_id, 'post_title' => $meta['name'], 'post_status' => $meta['status'])); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_update_discount', $meta, $discount_id); // Discount code updated return $discount_id; } else { // Add the discount $meta = apply_filters('edd_insert_discount', $meta); do_action('edd_pre_insert_discount', $meta); $discount_id = wp_insert_post(array('post_type' => 'edd_discount', 'post_title' => $meta['name'], 'post_status' => 'active')); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } /** * Fires after the discount code is inserted. * * @param array $meta { * The discount details. * * @type string $code The discount code. * @type string $name The name of the discount. * @type string $status The discount status. Defaults to active. * @type int $uses The current number of uses. * @type int $max_uses The max number of uses. * @type string $start The start date. * @type int $min_price The minimum price required to use the discount code. * @type array $product_reqs The product IDs required to use the discount code. * @type string $product_condition The conditions in which a product(s) must meet to use the discount code. * @type array $excluded_products Product IDs excluded from this discount code. * @type bool $is_not_global If the discount code is not globally applied to all products. Defaults to false. * @type bool $is_single_use If the code cannot be used more than once per customer. Defaults to false. * } * @param int $discount_id The ID of the discount that was inserted. */ do_action('edd_post_insert_discount', $meta, $discount_id); // Discount code created return $discount_id; } }
/** * Store Discount * * Stores a discount code. * If the code exists, it updates it, otherwise it creates a new one. * * @param string $discount_details * @param int $discount_id * * @access public * @since 1.0 * @return boolean */ function edd_store_discount($discount_details, $discount_id = null) { if (edd_discount_exists($discount_id) && !is_null($discount_id)) { // update an existing discount $discount_details = apply_filters('edd_update_discount', $discount_details, $discount_id); do_action('edd_pre_update_discount', $discount_details, $discount_id); wp_update_post(array('ID' => $discount_id, 'post_title' => $discount_details['name'], 'post_status' => $discount_details['status'])); $meta = array('code' => isset($discount_details['code']) ? $discount_details['code'] : '', 'uses' => isset($discount_details['uses']) ? $discount_details['uses'] : '', 'max_uses' => isset($discount_details['max']) ? $discount_details['max'] : '', 'amount' => isset($discount_details['amount']) ? $discount_details['amount'] : '', 'start' => isset($discount_details['start']) ? $discount_details['start'] : '', 'expiration' => isset($discount_details['expiration']) ? $discount_details['expiration'] : '', 'type' => isset($discount_details['type']) ? $discount_details['type'] : '', 'min_price' => isset($discount_details['min_price']) ? $discount_details['min_price'] : ''); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_update_discount', $discount_details, $discount_id); // discount code updated return true; } else { // add the discount $discount_details = apply_filters('edd_insert_discount', $discount_details); do_action('edd_pre_insert_discount', $discount_details); $discount_id = wp_insert_post(array('post_type' => 'edd_discount', 'post_title' => isset($discount_details['name']) ? $discount_details['name'] : '', 'post_status' => 'active')); $meta = array('code' => isset($discount_details['code']) ? $discount_details['code'] : '', 'uses' => isset($discount_details['uses']) ? $discount_details['uses'] : '', 'max_uses' => isset($discount_details['max']) ? $discount_details['max'] : '', 'amount' => isset($discount_details['amount']) ? $discount_details['amount'] : '', 'start' => isset($discount_details['start']) ? $discount_details['start'] : '', 'expiration' => isset($discount_details['expiration']) ? $discount_details['expiration'] : '', 'type' => isset($discount_details['type']) ? $discount_details['type'] : '', 'min_price' => isset($discount_details['min_price']) ? $discount_details['min_price'] : ''); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_insert_discount', $discount_details, $discount_id); // discount code created return true; } }
/** * Store Discount * * Stores a discount code. * If the code exists, it updates it, otherwise it creates a new one. * * @access public * @since 1.0 * @return boolean */ function edd_store_discount($discount_details, $id = null) { if (edd_discount_exists($id) && !is_null($id)) { // update an existing discount $discounts = edd_get_discounts(); if (!$discounts) { $discounts = array(); } $discounts[$id] = $discount_details; apply_filters('edd_update_discount', $discount_details, $id); update_option('edd_discounts', $discounts); do_action('edd_post_update_discount', $discount_details, $id); // discount code updated return true; } else { // add the discount $discounts = edd_get_discounts(); if (!$discounts) { $discounts = array(); } $discounts[] = $discount_details; apply_filters('edd_insert_discount', $discount_details); update_option('edd_discounts', $discounts); do_action('edd_post_insert_discount', $discount_details); // discount code created return true; } // something went wrong return false; }
/** * Stores a discount code. If the code already exists, it updates it, otherwise * it creates a new one. * * @since 1.0 * @param string $details * @param int $discount_id * @return bool Whether or not discount code was created */ function edd_store_discount($details, $discount_id = null) { $meta = array('code' => isset($details['code']) ? $details['code'] : '', 'uses' => isset($details['uses']) ? $details['uses'] : '', 'max_uses' => isset($details['max']) ? $details['max'] : '', 'amount' => isset($details['amount']) ? $details['amount'] : '', 'start' => isset($details['start']) ? $details['start'] : false, 'expiration' => isset($details['expiration']) ? $details['expiration'] : false, 'type' => isset($details['type']) ? $details['type'] : '', 'min_price' => isset($details['min_price']) ? $details['min_price'] : '', 'product_reqs' => isset($details['products']) ? $details['products'] : array(), 'product_condition' => isset($details['product_condition']) ? $details['product_condition'] : '', 'is_not_global' => isset($details['not_global']) ? $details['not_global'] : false, 'is_single_use' => isset($details['use_once']) ? $details['use_once'] : false); if ($meta['start']) { $meta['start'] = date('m/d/Y H:i:s', strtotime($meta['start'])); } if ($meta['expiration']) { $meta['expiration'] = date('m/d/Y H:i:s', strtotime(date('m/d/Y', strtotime($meta['expiration'])) . ' 23:59:59')); } if (edd_discount_exists($discount_id) && !empty($discount_id)) { // Update an existing discount $details = apply_filters('edd_update_discount', $details, $discount_id); do_action('edd_pre_update_discount', $details, $discount_id); wp_update_post(array('ID' => $discount_id, 'post_title' => $details['name'], 'post_status' => $details['status'])); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_update_discount', $details, $discount_id); // Discount code updated return true; } else { // Add the discount $details = apply_filters('edd_insert_discount', $details); do_action('edd_pre_insert_discount', $details); $discount_id = wp_insert_post(array('post_type' => 'edd_discount', 'post_title' => isset($details['name']) ? $details['name'] : '', 'post_status' => 'active')); foreach ($meta as $key => $value) { update_post_meta($discount_id, '_edd_discount_' . $key, $value); } do_action('edd_post_insert_discount', $details, $discount_id); // Discount code created return true; } }