/**
 * AJAX Remove From Cart
 *
 * Removes item from cart.
 *
 * @access      private
 * @since       1.0
 * @return      string
*/
function edd_ajax_remove_from_cart()
{
    if (isset($_POST['cart_item']) && check_ajax_referer('edd_ajax_nonce', 'nonce')) {
        edd_remove_from_cart($_POST['cart_item']);
        echo 'removed';
    }
    die;
}
/**
 * Removes item from cart via AJAX.
 *
 * @since 1.0
 * @return void
 */
function edd_ajax_remove_from_cart()
{
    if (isset($_POST['cart_item'])) {
        edd_remove_from_cart($_POST['cart_item']);
        $return = array('removed' => 1, 'subtotal' => html_entity_decode(edd_currency_filter(edd_format_amount(edd_get_cart_subtotal())), ENT_COMPAT, 'UTF-8'), 'total' => html_entity_decode(edd_currency_filter(edd_format_amount(edd_get_cart_total())), ENT_COMPAT, 'UTF-8'));
        echo json_encode($return);
    }
    edd_die();
}
Exemplo n.º 3
0
 /**
  * Updates a product variation to a different one
  */
 public function update_variation()
 {
     $download_id = filter_input(INPUT_POST, 'download_id');
     $price_id = filter_input(INPUT_POST, 'price_id');
     if (false === $download_id || false === $price_id) {
         echo wp_json_encode(['status' => 'error', 'error' => 'No download_id and price_id provided.']);
         wp_die();
     }
     $old_download = edd_get_item_position_in_cart($download_id);
     // Add first, then remove, to keep discount codes that might apply.
     edd_add_to_cart($download_id, array('price_id' => $price_id));
     edd_remove_from_cart($old_download);
     echo wp_json_encode(['status' => 'success']);
     wp_die();
 }
Exemplo n.º 4
0
/**
 * Process the Remove from Cart request
 *
 * @since 1.0
 *
 * @param $data
 */
function edd_process_remove_from_cart($data)
{
    $cart_key = absint($_GET['cart_item']);
    edd_remove_from_cart($cart_key);
    wp_redirect(remove_query_arg(array('edd_action', 'cart_item', 'nocache')));
    edd_die();
}
/**
 * Removes item from cart via AJAX.
 *
 * @since 1.0
 * @return void
 */
function edd_ajax_remove_from_cart()
{
    if (isset($_POST['cart_item'])) {
        edd_remove_from_cart($_POST['cart_item']);
        $return = array('removed' => 1, 'subtotal' => html_entity_decode(edd_currency_filter(edd_format_amount(edd_get_cart_subtotal())), ENT_COMPAT, 'UTF-8'), 'total' => html_entity_decode(edd_currency_filter(edd_format_amount(edd_get_cart_total())), ENT_COMPAT, 'UTF-8'), 'cart_quantity' => html_entity_decode(edd_get_cart_quantity()));
        if (edd_use_taxes()) {
            $cart_tax = (double) edd_get_cart_tax();
            $return['tax'] = html_entity_decode(edd_currency_filter(edd_format_amount($cart_tax)), ENT_COMPAT, 'UTF-8');
        }
        echo json_encode($return);
    }
    edd_die();
}
Exemplo n.º 6
0
/**
 * Remove From Cart
 *
 * Process the remove form cart request.
 *
 * @access      private
 * @since       1.0
 * @return      void
*/
function edd_process_remove_from_cart($data)
{
    $cart_key = $_GET['cart_item'];
    $cart = edd_remove_from_cart($cart_key);
}
Exemplo n.º 7
0
function edd_downloads_renew_to_cart($download_id, $options)
{
    if (!edd_sl_renewals_allowed()) {
        return;
    }
    $post_data = urldecode($_REQUEST['post_data']);
    $post_data_formated = array();
    if (strstr($post_data, "expired")) {
        $post_data_split = explode("&", $post_data);
        foreach ($post_data_split as $data_split) {
            $data_array = explode("=", $data_split);
            $post_data_formated[$data_array[0]] = $data_array[1];
        }
    }
    if (isset($post_data_formated['expired']) && $post_data_formated['expired'] == 1 && isset($post_data_formated['license_id']) && isset($post_data_formated['product_id'])) {
        global $edd_options;
        $license_key = edd_software_licensing()->get_license_key($post_data_formated['license_id']);
        $data = array('edd_license_key' => $license_key, 'edd_action' => 'apply_license_renewal');
        $license = !empty($data['edd_license_key']) ? sanitize_text_field($data['edd_license_key']) : false;
        $valid = true;
        if (!$license) {
            $valid = false;
        }
        $license_id = edd_software_licensing()->get_license_by_key($license);
        if (empty($license_id)) {
            $valid = false;
        }
        $download_id = get_post_meta($license_id, '_edd_sl_download_id', true);
        if (empty($download_id) || !edd_item_in_cart($download_id)) {
            $valid = false;
        }
        $options = array();
        // if product has variable prices, find previous used price id and add it to cart
        if (edd_has_variable_prices($download_id)) {
            $price_id = get_post_meta($license_id, '_edd_sl_download_price_id', true);
            if ('' === $price_id) {
                // If no $price_id is available, try and find it from the payment ID. See https://github.com/pippinsplugins/EDD-Software-Licensing/issues/110
                $payment_id = get_post_meta($license_id, '_edd_sl_payment_id', true);
                $payment_items = edd_get_payment_meta_downloads($payment_id);
                foreach ($payment_items as $payment_item) {
                    if ((int) $payment_item['id'] !== (int) $download_id) {
                        continue;
                    }
                    if (isset($payment_item['options']['price_id'])) {
                        $options['price_id'] = $payment_item['options']['price_id'];
                        break;
                    }
                }
            } else {
                $options['price_id'] = $price_id;
            }
            $cart_key = edd_get_item_position_in_cart($download_id, $options);
            edd_remove_from_cart($cart_key);
            edd_add_to_cart($download_id, $options);
            $valid = true;
        }
        if ($valid) {
            $keys = (array) EDD()->session->get('edd_renewal_keys');
            $keys[$download_id] = $license;
            EDD()->session->set('edd_is_renewal', '1');
            EDD()->session->set('edd_renewal_keys', $keys);
            //$redirect = edd_get_checkout_uri();
        }
        //do_action('edd_apply_license_renewal', array('edd_license_key' => $license_key ,'edd_action' => 'apply_license_renewal'));
    }
    return true;
}
Exemplo n.º 8
0
/**
 * Adds a license key renewal to the cart
 *
 * @since  3.4
 * @param  integer       $license_id The ID of the license key to add
 * @param  bool          $by_key     Set to true if passing actual license key as $license_id
 * @return bool|WP_Error $success    True if the renewal was added to the cart, WP_Error is not successful
 */
function edd_sl_add_renewal_to_cart($license_id = 0, $by_key = false)
{
    if ($by_key) {
        $license_key = $license_id;
        $license_id = edd_software_licensing()->get_license_by_key($license_id);
    } else {
        $license_key = edd_software_licensing()->get_license_key($license_id);
    }
    if (empty($license_id)) {
        return new WP_Error('missing_license', __('No license ID supplied or invalid key provided', 'edd_sl'));
    }
    $license_post = get_post($license_id);
    if (!$license_post) {
        return new WP_Error('missing_license', __('No license ID supplied or invalid key provided', 'edd_sl'));
    }
    $success = false;
    $payment_id = get_post_meta($license_id, '_edd_sl_payment_id', true);
    $payment = get_post($payment_id);
    $download_id = edd_software_licensing()->get_download_id($license_id);
    $download = get_post($download_id);
    if ('publish' !== $payment->post_status && 'complete' !== $payment->post_status) {
        return new WP_Error('payment_not_complete', __('The purchase record for this license is not marked as complete', 'edd_sl'));
    }
    if ('publish' !== $license_post->post_status) {
        return new WP_Error('license_disabled', __('The supplied license has been disabled and cannot be renewed', 'edd_sl'));
    }
    if ('publish' !== $download->post_status) {
        return new WP_Error('license_disabled', __('The download for this license is not published', 'edd_sl'));
    }
    $license_parent = !empty($license_post->post_parent) ? get_post($license_post->post_parent) : false;
    // This license key is part of a bundle, setup the parent
    if ($license_post->post_parent && !empty($license_parent)) {
        $parent_license_id = $license_parent->ID;
        $parent_download_id = edd_software_licensing()->get_download_id($parent_license_id);
        $parent_license_key = edd_software_licensing()->get_license_key($parent_license_id);
        if (!edd_item_in_cart($parent_download_id) && !edd_has_variable_prices($download_id)) {
            edd_add_to_cart($parent_download_id);
        }
        $license_id = $parent_license_id;
        $license_key = edd_software_licensing()->get_license_key($parent_license_id);
        $license = $parent_license_key;
        $download_id = $parent_download_id;
    }
    $options = array('is_renewal' => true);
    // if product has variable prices, find previous used price id and add it to cart
    if (edd_has_variable_prices($download_id)) {
        $price_id = edd_software_licensing()->get_price_id($license_id);
        if ('' === $price_id) {
            // If no $price_id is available, try and find it from the payment ID. See https://github.com/pippinsplugins/EDD-Software-Licensing/issues/110
            $payment_items = edd_get_payment_meta_downloads($payment_id);
            foreach ($payment_items as $payment_item) {
                if ((int) $payment_item['id'] !== (int) $download_id) {
                    continue;
                }
                if (isset($payment_item['options']['price_id'])) {
                    $options['price_id'] = $payment_item['options']['price_id'];
                    break;
                }
            }
        } else {
            $options['price_id'] = $price_id;
        }
    }
    if (empty($download_id)) {
        return new WP_Error('no_download_id', __('There does not appear to be a download ID attached to this license key', 'edd_sl'));
    }
    // Make sure it's not already in the cart
    $cart_key = edd_get_item_position_in_cart($download_id, $options);
    if (edd_item_in_cart($download_id, $options) && false !== $cart_key) {
        edd_remove_from_cart($cart_key);
    }
    edd_add_to_cart($download_id, $options);
    $success = true;
    // Confirm item was added to cart successfully
    if (!edd_item_in_cart($download_id, $options)) {
        return new WP_Error('not_in_cart', __('The download for this license is not in the cart or could not be added', 'edd_sl'));
    }
    // Get updated cart key
    $cart_key = edd_get_item_position_in_cart($download_id, $options);
    if (true === $success) {
        $keys = edd_sl_get_renewal_keys();
        $keys[$cart_key] = $license_key;
        EDD()->session->set('edd_is_renewal', '1');
        EDD()->session->set('edd_renewal_keys', $keys);
        return true;
    }
    return new WP_Error('renewal_error', __('Something went wrong while attempting to apply the renewal', 'edd_sl'));
}