/** * 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; }
/** * 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; }
/** * 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(); }
/** * 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; }
/** * 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; }
/** * 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(); }
/** * Discounts Page * * Renders the discount page contents. * * @access private * @since 1.0 * @return void */ function edd_discounts_page() { global $edd_options; $current_page = get_bloginfo('wpurl') . '/wp-admin/admin.php?edit.php?post_type=download&page=edd-discounts'; ?> <div class="wrap"> <?php if (isset($_GET['edd-action']) && $_GET['edd-action'] == 'edit_discount') { ?> <?php include_once EDD_PLUGIN_DIR . 'includes/admin-pages/forms/edit-discount.php'; ?> <?php } else { ?> <h2><?php _e('Discount Codes', 'edd'); ?> </h2> <?php $discounts = edd_get_discounts(); ?> <table class="wp-list-table widefat fixed posts edd-discounts"> <thead> <tr> <th><?php _e('Name', 'edd'); ?> </th> <th><?php _e('Code', 'edd'); ?> </th> <th><?php _e('Amount', 'edd'); ?> </th> <th><?php _e('Uses', 'edd'); ?> </th> <th><?php _e('Max Uses', 'edd'); ?> </th> <th><?php _e('Start Date', 'edd'); ?> </th> <th><?php _e('Expiration', 'edd'); ?> </th> <th><?php _e('Status', 'edd'); ?> </th> <th><?php _e('Actions', 'edd'); ?> </th> </tr> </thead> <tfoot> <tr> <th><?php _e('Name', 'edd'); ?> </th> <th><?php _e('Code', 'edd'); ?> </th> <th><?php _e('Amount', 'edd'); ?> </th> <th><?php _e('Uses', 'edd'); ?> </th> <th><?php _e('Max Uses', 'edd'); ?> </th> <th><?php _e('Start Date', 'edd'); ?> </th> <th><?php _e('Expiration', 'edd'); ?> </th> <th><?php _e('Status', 'edd'); ?> </th> <th><?php _e('Actions', 'edd'); ?> </th> </tr> </tfoot> <tbody> <?php if ($discounts) { ?> <?php foreach ($discounts as $discount_key => $discount) { ?> <tr> <td><?php if (isset($discount['name'])) { echo $discount['name']; } ?> </td> <td><?php if (isset($discount['code'])) { echo $discount['code']; } ?> </td> <td><?php if (isset($discount['amount'])) { echo edd_format_discount_rate($discount['type'], $discount['amount']); } ?> </td> <td> <?php if (isset($discount['uses']) && isset($discount['max']) && $discount['uses'] != '' && $discount['max'] != '') { echo $discount['uses'] == '' ? 0 : $discount['uses'] . '/' . $discount['max']; } else { echo isset($discount['uses']) ? $discount['uses'] : 0; } ?> </td> <td> <?php if (isset($discount['max'])) { echo $discount['max'] == '' ? __('unlimited', 'edd') : $discount['max']; } else { _e('unlimited', 'edd'); } ?> </td> <td> <?php if (isset($discount['start']) && $discount['start'] != '') { echo date(get_option('date_format'), strtotime($discount['start'])); } else { _e('No start date', 'edd'); } ?> </td> <td> <?php if (isset($discount['expiration']) && $discount['expiration'] != '') { echo edd_is_discount_expired($discount_key) ? __('Expired', 'edd') : $discount['expiration']; } else { _e('no expiration', 'edd'); } ?> </td> <td><?php if (isset($discount['status'])) { echo $discount['status']; } ?> </td> <td> <a href="<?php echo add_query_arg('edd-action', 'edit_discount', add_query_arg('discount', $discount_key, $current_page)); ?> "><?php _e('Edit', 'edd'); ?> </a> | <?php if (edd_is_discount_active($discount_key)) { ?> <a href="<?php echo add_query_arg('edd-action', 'deactivate_discount', add_query_arg('discount', $discount_key, $current_page)); ?> "><?php _e('Deactivate', 'edd'); ?> </a> | <?php } else { ?> <a href="<?php echo add_query_arg('edd-action', 'activate_discount', add_query_arg('discount', $discount_key, $current_page)); ?> "><?php _e('Activate', 'edd'); ?> </a> | <?php } ?> <a href="<?php echo add_query_arg('edd-action', 'delete_discount', add_query_arg('discount', $discount_key, $current_page)); ?> "><?php _e('Delete', 'edd'); ?> </a> </td> </tr> <?php } ?> <?php } else { ?> <tr><td colspan=10><?php _e('No discount codes have been created.', 'edd'); ?> </td> <?php } ?> </tbody> </table> <?php do_action('edd_discounts_below_table'); ?> <?php include_once EDD_PLUGIN_DIR . 'includes/admin-pages/forms/add-discount.php'; ?> <?php } ?> </div><!--end wrap--> <?php }