/** * Process Get Discounts API Request * * @access public * @since 1.6 * @global object $wpdb Used to query the database using the WordPress * Database API * @param int $discount Discount ID * @return array $discounts Multidimensional array of the discounts */ public function get_discounts($discount = null) { $discount_list = array(); if (!user_can($this->user_id, 'manage_shop_discounts') && !$this->override) { return $discount_list; } $error = array(); if (empty($discount)) { global $wpdb; $paged = $this->get_paged(); $per_page = $this->per_page(); $discounts = edd_get_discounts(array('posts_per_page' => $per_page, 'paged' => $paged)); $count = 0; if (empty($discounts)) { $error['error'] = __('No discounts found!', 'easy-digital-downloads'); return $error; } foreach ($discounts as $discount) { $discount_list['discounts'][$count]['ID'] = $discount->ID; $discount_list['discounts'][$count]['name'] = $discount->post_title; $discount_list['discounts'][$count]['code'] = edd_get_discount_code($discount->ID); $discount_list['discounts'][$count]['amount'] = edd_get_discount_amount($discount->ID); $discount_list['discounts'][$count]['min_price'] = edd_get_discount_min_price($discount->ID); $discount_list['discounts'][$count]['type'] = edd_get_discount_type($discount->ID); $discount_list['discounts'][$count]['uses'] = edd_get_discount_uses($discount->ID); $discount_list['discounts'][$count]['max_uses'] = edd_get_discount_max_uses($discount->ID); $discount_list['discounts'][$count]['start_date'] = edd_get_discount_start_date($discount->ID); $discount_list['discounts'][$count]['exp_date'] = edd_get_discount_expiration($discount->ID); $discount_list['discounts'][$count]['status'] = $discount->post_status; $discount_list['discounts'][$count]['product_requirements'] = edd_get_discount_product_reqs($discount->ID); $discount_list['discounts'][$count]['requirement_condition'] = edd_get_discount_product_condition($discount->ID); $discount_list['discounts'][$count]['global_discount'] = edd_is_discount_not_global($discount->ID); $discount_list['discounts'][$count]['single_use'] = edd_discount_is_single_use($discount->ID); $count++; } } else { if (is_numeric($discount) && get_post($discount)) { $discount_list['discounts'][0]['ID'] = $discount; $discount_list['discounts'][0]['name'] = get_post_field('post_title', $discount); $discount_list['discounts'][0]['code'] = edd_get_discount_code($discount); $discount_list['discounts'][0]['amount'] = edd_get_discount_amount($discount); $discount_list['discounts'][0]['min_price'] = edd_get_discount_min_price($discount); $discount_list['discounts'][0]['type'] = edd_get_discount_type($discount); $discount_list['discounts'][0]['uses'] = edd_get_discount_uses($discount); $discount_list['discounts'][0]['max_uses'] = edd_get_discount_max_uses($discount); $discount_list['discounts'][0]['start_date'] = edd_get_discount_start_date($discount); $discount_list['discounts'][0]['exp_date'] = edd_get_discount_expiration($discount); $discount_list['discounts'][0]['status'] = get_post_field('post_status', $discount); $discount_list['discounts'][0]['product_requirements'] = edd_get_discount_product_reqs($discount); $discount_list['discounts'][0]['requirement_condition'] = edd_get_discount_product_condition($discount); $discount_list['discounts'][0]['global_discount'] = edd_is_discount_not_global($discount); $discount_list['discounts'][0]['single_use'] = edd_discount_is_single_use($discount); } else { $error['error'] = sprintf(__('Discount %s not found!', 'easy-digital-downloads'), $discount); return $error; } } return $discount_list; }
/** * Discounts short code * * Displays a list of all the active discounts. The active discounts can be configured * from the Discount Codes admin screen. * * @since 1.0.8.2 * @param array $atts Shortcode attributes * @param string $content * @uses edd_get_discounts() * @return string $discounts_lists List of all the active discount codes */ function edd_discounts_shortcode($atts, $content = null) { $discounts = edd_get_discounts(); if (!$discounts && edd_has_active_discounts()) { return; } $discounts_list = '<ul id="edd_discounts_list">'; foreach ($discounts as $discount) { if (edd_is_discount_active($discount->ID)) { $discounts_list .= '<li class="edd_discount">'; $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code($discount->ID) . '</span>'; $discounts_list .= '<span class="edd_discount_separator"> - </span>'; $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)) . '</span>'; $discounts_list .= '</li>'; } } $discounts_list .= '</ul>'; return $discounts_list; }
/** * Show the fully formatted cart discount * * @since 1.4.1 * @param bool $formatted * @param bool $echo Echo? * @return string $amount Fully formatted cart discount */ function edd_display_cart_discount($formatted = false, $echo = false) { $discounts = edd_get_cart_discounts(); if (empty($discounts)) { return false; } $discount_id = edd_get_discount_id_by_code($discounts[0]); $amount = edd_format_discount_rate(edd_get_discount_type($discount_id), edd_get_discount_amount($discount_id)); if ($echo) { echo $amount; } return $amount; }
/** * Discounts short code * * Displays a list of all the active discounts. The active discounts can be configured * from the Discount Codes admin screen. * * @since 1.0.8.2 * @param array $atts Shortcode attributes * @param string $content * @uses edd_get_discounts() * @return string $discounts_lists List of all the active discount codes */ function edd_discounts_shortcode($atts, $content = null) { $discounts = edd_get_discounts(); $discounts_list = '<ul id="edd_discounts_list">'; if (!empty($discounts) && edd_has_active_discounts()) { foreach ($discounts as $discount) { if (edd_is_discount_active($discount->ID)) { $discounts_list .= '<li class="edd_discount">'; $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code($discount->ID) . '</span>'; $discounts_list .= '<span class="edd_discount_separator"> - </span>'; $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)) . '</span>'; $discounts_list .= '</li>'; } } } else { $discounts_list .= '<li class="edd_discount">' . __('No discounts found', 'easy-digital-downloads') . '</li>'; } $discounts_list .= '</ul>'; return $discounts_list; }
</p> </td> </tr> <?php do_action('edd_edit_discount_form_before_amount', $discount_id, $discount); ?> <tr> <th scope="row" valign="top"> <label for="edd-amount"><?php _e('Amount', 'edd'); ?> </label> </th> <td> <input type="text" id="edd-amount" name="amount" value="<?php echo esc_attr(edd_get_discount_amount($discount_id)); ?> " style="width: 40px;"/> <p class="description edd-amount-description"<?php echo $flat_display; ?> ><?php printf(__('Enter the discount amount in %s', 'edd'), edd_get_currency()); ?> </p> <p class="description edd-amount-description"<?php echo $percent_display; ?> ><?php _e('Enter the discount percentage. 10 = 10%', 'edd'); ?>
/** * Validates the supplied discount sent via AJAX. * * @since 1.0 * @return void */ function edd_ajax_apply_discount() { if (isset($_POST['code'])) { $discount_code = sanitize_text_field($_POST['code']); $return = array('msg' => '', 'code' => $discount_code); $user = ''; if (is_user_logged_in()) { $user = get_current_user_id(); } else { parse_str($_POST['form'], $form); if (!empty($form['edd_email'])) { $user = urldecode($form['edd_email']); } } if (edd_is_discount_valid($discount_code, $user)) { $discount = edd_get_discount_by_code($discount_code); $amount = edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)); $discounts = edd_set_cart_discount($discount_code); $total = edd_get_cart_total($discounts); $return = array('msg' => 'valid', 'amount' => $amount, 'total_plain' => $total, 'total' => html_entity_decode(edd_currency_filter(edd_format_amount($total)), ENT_COMPAT, 'UTF-8'), 'code' => $discount_code, 'html' => edd_get_cart_discounts_html($discounts)); } else { $errors = edd_get_errors(); $return['msg'] = $errors['edd-discount-error']; edd_unset_error('edd-discount-error'); } // Allow for custom discount code handling $return = apply_filters('edd_ajax_discount_response', $return); echo json_encode($return); } edd_die(); }
/** * Retrieve all the data for all the discount codes * * @access public * @since 1.4 * @return array $discount_codes_data Array of all the data for the discount codes */ public function discount_codes_data() { $discount_codes_data = array(); $per_page = $this->per_page; $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'ID'; $order = isset($_GET['order']) ? $_GET['order'] : 'DESC'; $status = isset($_GET['status']) ? $_GET['status'] : array('active', 'inactive'); $meta_key = isset($_GET['meta_key']) ? $_GET['meta_key'] : null; $search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : null; $discounts = edd_get_discounts(array('posts_per_page' => $per_page, 'paged' => isset($_GET['paged']) ? $_GET['paged'] : 1, 'orderby' => $orderby, 'order' => $order, 'post_status' => $status, 'meta_key' => $meta_key, 's' => $search)); if ($discounts) { foreach ($discounts as $discount) { if (edd_get_discount_max_uses($discount->ID)) { $uses = edd_get_discount_uses($discount->ID) . '/' . edd_get_discount_max_uses($discount->ID); } else { $uses = edd_get_discount_uses($discount->ID); } if (edd_get_discount_max_uses($discount->ID)) { $max_uses = edd_get_discount_max_uses($discount->ID) ? edd_get_discount_max_uses($discount->ID) : __('unlimited', 'edd'); } else { $max_uses = __('Unlimited', 'edd'); } $start_date = edd_get_discount_start_date($discount->ID); if (!empty($start_date)) { $discount_start_date = date_i18n(get_option('date_format'), strtotime($start_date)); } else { $discount_start_date = __('No start date', 'edd'); } if (edd_get_discount_expiration($discount->ID)) { $expiration = date_i18n(get_option('date_format'), strtotime(edd_get_discount_expiration($discount->ID))); } else { $expiration = __('No expiration', 'edd'); } $discount_codes_data[] = array('ID' => $discount->ID, 'name' => get_the_title($discount->ID), 'code' => edd_get_discount_code($discount->ID), 'amount' => edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)), 'uses' => $uses, 'max_uses' => $max_uses, 'start_date' => $discount_start_date, 'expiration' => $expiration, 'status' => edd_is_discount_expired($discount->ID) ? 'expired' : $discount->post_status); } } return $discount_codes_data; }
/** * Validates the supplied discount sent via AJAX. * * @since 1.0 * @return void */ function edd_ajax_apply_discount() { if (isset($_POST['code']) && check_ajax_referer('edd_checkout_nonce', 'nonce')) { $user = isset($_POST['user']) ? $_POST['user'] : $_POST['email']; $return = array('msg' => '', 'code' => $_POST['code']); if (edd_is_discount_used($_POST['code'], $user)) { // Called twice if discount is not used (again by edd_is_discount_valid) but allows for beter usr msg and less execution if discount is used. $return['msg'] = __('This discount code has been used already', 'edd'); } else { if (edd_is_discount_valid($_POST['code'], $user)) { $discount = edd_get_discount_by_code($_POST['code']); $amount = edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)); $discounts = edd_set_cart_discount($_POST['code']); $total = edd_get_cart_total($discounts); $return = array('msg' => 'valid', 'amount' => $amount, 'total' => html_entity_decode(edd_currency_filter(edd_format_amount($total)), ENT_COMPAT, 'UTF-8'), 'code' => $_POST['code'], 'html' => edd_get_cart_discounts_html($discounts)); } else { $return['msg'] = __('The discount you entered is invalid', 'edd'); } } echo json_encode($return); } edd_die(); }
/** * Discount remove icon */ function pp_edd_get_cart_discounts_html($html, $discounts, $rate, $remove_url) { if (!$discounts) { $discounts = edd_get_cart_discounts(); } if (!$discounts) { return; } $html = ''; foreach ($discounts as $discount) { $discount_id = edd_get_discount_id_by_code($discount); $rate = edd_format_discount_rate(edd_get_discount_type($discount_id), edd_get_discount_amount($discount_id)); $remove_url = add_query_arg(array('edd_action' => 'remove_cart_discount', 'discount_id' => $discount_id, 'discount_code' => $discount), edd_get_checkout_uri()); $svg = '<svg width="16" height="16" viewBox="0 0 16 16"><use xlink:href="' . get_stylesheet_directory_uri() . "/images/svg-defs.svg#icon-remove" . '"></use></svg>'; $html .= "<span class=\"edd_discount\">\n"; $html .= "<span class=\"edd_discount_rate\">{$discount} – {$rate}</span>\n"; $html .= "<a href=\"{$remove_url}\" data-code=\"{$discount}\" class=\"edd_discount_remove\">{$svg}</a>\n"; $html .= "</span>\n"; } return $html; }
/** * Get Discounted Amount * * Gets the discounted price. * * @access public * @since 1.0 * @param string $code the code to calculate a discount for * @param string|int $base_price the price before discount * @return string $discounted_price the amount after discount */ function edd_get_discounted_amount($code, $base_price) { $discount_id = edd_get_discount_id_by_code($code); $type = edd_get_discount_type($discount_id); $rate = edd_get_discount_amount($discount_id); if ($type == 'flat') { // set amount $discounted_price = $base_price - $rate; if ($discounted_price < 0) { $discounted_price = 0; } } else { // percentage discount $discounted_price = $base_price - $base_price * ($rate / 100); } return apply_filters('edd_discounted_amount', number_format($discounted_price, 2)); }
/** * Validates the supplied discount sent via AJAX. * * @since 1.0 * @return void */ function edd_ajax_apply_discount() { if (isset($_POST['code'])) { $discount_code = $_POST['code']; $return = array('msg' => '', 'code' => $discount_code); if (edd_is_discount_valid($discount_code)) { $discount = edd_get_discount_by_code($discount_code); $amount = edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)); $discounts = edd_set_cart_discount($discount_code); $total = edd_get_cart_total($discounts); $return = array('msg' => 'valid', 'amount' => $amount, 'total_plain' => $total, 'total' => html_entity_decode(edd_currency_filter(edd_format_amount($total)), ENT_COMPAT, 'UTF-8'), 'code' => $_POST['code'], 'html' => edd_get_cart_discounts_html($discounts)); } else { $errors = edd_get_errors(); $return['msg'] = $errors['edd-discount-error']; edd_unset_error('edd-discount-error'); } // Allow for custom discount code handling $return = apply_filters('edd_ajax_discount_response', $return); echo json_encode($return); } edd_die(); }