/**
 * Outputs the HTML for all discounts applied to the cart
 *
 * @since 1.4.1
 * @return void
 */
function edd_cart_discounts_html()
{
    echo edd_get_cart_discounts_html();
}
/**
 * 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();
}
/**
 * Removes a discount code from the cart via ajax
 *
 * @since 1.7
 * @return void
 */
function edd_ajax_remove_discount()
{
    if (isset($_POST['code'])) {
        edd_unset_cart_discount(urldecode($_POST['code']));
        $total = edd_get_cart_total();
        $return = array('total' => html_entity_decode(edd_currency_filter(edd_format_amount($total)), ENT_COMPAT, 'UTF-8'), 'code' => $_POST['code'], 'discounts' => edd_get_cart_discounts(), 'html' => edd_get_cart_discounts_html());
        echo json_encode($return);
    }
    edd_die();
}
 /**
  * Load discount
  *
  * @since 2.0
  */
 public function share_product()
 {
     if (!isset($_POST['product_id'])) {
         return;
     }
     // check nonce
     check_ajax_referer('edd_sd_nonce', 'nonce');
     global $edd_options;
     // get discount code's ID from plugin settings
     $discount = edd_get_option('edd_sd_discount_code', '');
     // get discount code by ID
     $discount = edd_get_discount_code($discount);
     // set cart discount. Discount will only be applied if discount exists.
     $discounts = edd_set_cart_discount($discount);
     $total = edd_get_cart_total($discounts);
     // purchase was shared
     EDD()->session->set('edd_shared', true);
     // store the download ID temporarily
     EDD()->session->set('edd_shared_id', $_POST['product_id']);
     $return = apply_filters('edd_social_discounts_ajax_return', array('msg' => 'valid', 'success_title' => $this->success_title(), 'success_message' => $this->success_message($_POST['product_id']), 'product_id' => $_POST['product_id'], 'total' => html_entity_decode(edd_currency_filter(edd_format_amount($total)), ENT_COMPAT, 'UTF-8'), 'html' => edd_get_cart_discounts_html($discounts)));
     echo json_encode($return);
     edd_die();
 }