/**
 * AJAX Validate Discount
 *
 * Validates the supplied discount.
 *
 * @access      private
 * @since       1.0
 * @return      string
*/
function edd_ajax_validate_discount()
{
    if (isset($_POST['code']) && check_ajax_referer('edd_ajax_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)) {
                $price = edd_get_cart_amount();
                $discounted_price = edd_get_discounted_amount($_POST['code'], $price);
                $return = array('msg' => 'valid', 'amount' => edd_currency_filter(edd_format_amount($discounted_price)), 'code' => $_POST['code']);
            } else {
                $return['msg'] = __('The discount you entered is invalid', 'edd');
            }
        }
        echo json_encode($return);
    }
    die;
}
Ejemplo n.º 2
0
/**
 * Discounts short code
 *
 * Displays a list of all active discounts
 *
 * @access      public
 * @since       1.0.8.2
 * @return      string
*/
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_valid($discount['code'])) {
            $discounts_list .= '<li class="edd_discount">';
            $discounts_list .= '<span class="edd_discount_name">' . $discount['name'] . '</span>';
            $discounts_list .= '<span class="edd_discount_separator"> - </span>';
            $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate($discount['type'], $discount['amount']) . '</span>';
            $discounts_list .= '</li>';
        }
    }
    $discounts_list .= '</ul>';
    return $discounts_list;
}
/**
 * Applies the preset discount, if any. This is separated from edd_listen_for_cart_discount() in order to allow items to be
 * added to the cart and for it to persist across page loads if necessary
 *
 * @return void
 */
function edd_apply_preset_discount()
{
    $code = sanitize_text_field(EDD()->session->get('preset_discount'));
    if (!$code) {
        return;
    }
    if (!edd_is_discount_valid($code, '', false)) {
        return;
    }
    $code = apply_filters('edd_apply_preset_discount', $code);
    edd_set_cart_discount($code);
    EDD()->session->set('preset_discount', null);
}
/**
 * Purchase Form Validate Discounts
 *
 * @access      private
 * @since       1.0.8.1
 * @return      string
 */
function edd_purchase_form_validate_discounts()
{
    // Retrieve the discount stored in cookies
    $discounts = edd_get_cart_discounts();
    $user = '';
    if (isset($_POST['edd_user_login']) && !empty($_POST['edd_user_login'])) {
        $user = sanitize_text_field($_POST['edd_user_login']);
    } else {
        if (isset($_POST['edd_email']) && !empty($_POST['edd_email'])) {
            $user = sanitize_text_field($_POST['edd_email']);
        } else {
            if (is_user_logged_in()) {
                $user = wp_get_current_user()->user_email;
            }
        }
    }
    $error = false;
    // Check for valid discount(s) is present
    if (!empty($_POST['edd-discount']) && __('Enter discount', 'easy-digital-downloads') != $_POST['edd-discount']) {
        // Check for a posted discount
        $posted_discount = isset($_POST['edd-discount']) ? trim($_POST['edd-discount']) : false;
        // Add the posted discount to the discounts
        if ($posted_discount && (empty($discounts) || edd_multiple_discounts_allowed()) && edd_is_discount_valid($posted_discount, $user)) {
            edd_set_cart_discount($posted_discount);
        }
    }
    // If we have discounts, loop through them
    if (!empty($discounts)) {
        foreach ($discounts as $discount) {
            // Check if valid
            if (!edd_is_discount_valid($discount, $user)) {
                // Discount is not valid
                $error = true;
            }
        }
    } else {
        // No discounts
        return 'none';
    }
    if ($error) {
        edd_set_error('invalid_discount', __('One or more of the discounts you entered is invalid', 'easy-digital-downloads'));
    }
    return implode(', ', $discounts);
}
/**
 * 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();
}
/**
 * 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();
}
/**
 * Purchase Form Validate Discounts
 *
 * @access      private
 * @since       1.0.8.1
 * @return      string
*/
function edd_purchase_form_validate_discounts()
{
    // check for valid discount is present
    if (isset($_POST['edd-discount']) && trim($_POST['edd-discount']) != '') {
        // clean discount
        $discount = sanitize_text_field($_POST['edd-discount']);
        $user = isset($_POST['edd_user_login']) ? sanitize_text_field($_POST['edd_user_login']) : sanitize_email($_POST['edd_email']);
        // check if validates
        if (edd_is_discount_valid($discount, $user)) {
            // return clean discount
            return $discount;
            // invalid discount
        } else {
            // set invalid discount error
            edd_set_error('invalid_discount', __('The discount you entered is invalid', 'edd'));
        }
    }
    // return default value
    return 'none';
}
/**
 * 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();
}
/**
 * Checks whether discounts are still valid when removing items from the cart
 *
 * If a discount requires a certain product, and that product is no longer in the cart, the discount is removed
 *
 * @since 1.5.2
 * @return void
 */
function edd_maybe_remove_cart_discount($cart_key = 0)
{
    $discounts = edd_get_cart_discounts();
    if (!$discounts) {
        return;
    }
    foreach ($discounts as $discount) {
        if (!edd_is_discount_valid($discount)) {
            edd_unset_cart_discount($discount);
        }
    }
}
/**
 * Purchase Form Validate Discounts
 *
 * @access      private
 * @since       1.0.8.1
 * @return      string
 */
function edd_purchase_form_validate_discounts()
{
    // Retrieve the discount stored in cookies
    $discounts = edd_get_cart_discounts();
    // Check for valid discount is present
    if (!empty($_POST['edd-discount']) || $discounts !== false) {
        if (empty($discounts)) {
            $discount = sanitize_text_field($_POST['edd-discount']);
        } else {
            // Use the discount stored in the cookies
            $discount = $discounts[0];
            // Note: At some point this will support multiple discounts
        }
        $user = isset($_POST['edd_user_login']) ? sanitize_text_field($_POST['edd_user_login']) : sanitize_email($_POST['edd_email']);
        // Check if validates
        if (edd_is_discount_valid($discount, $user)) {
            // Return clean discount
            return $discount;
        } else {
            // Set invalid discount error
            edd_set_error('invalid_discount', __('The discount you entered is invalid', 'edd'));
        }
    }
    // Return default value
    return 'none';
}