Example #1
0
/**
 * Load Scripts
 *
 * Enqueues the required scripts.
 *
 * @since 1.0
 * @global $post
 * @return void
 */
function edd_load_scripts()
{
    global $post;
    $js_dir = EDD_PLUGIN_URL . 'assets/js/';
    // Use minified libraries if SCRIPT_DEBUG is turned off
    $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
    // Get position in cart of current download
    if (isset($post->ID)) {
        $position = edd_get_item_position_in_cart($post->ID);
    }
    if (edd_is_checkout()) {
        if (edd_is_cc_verify_enabled()) {
            wp_register_script('creditCardValidator', $js_dir . 'jquery.creditCardValidator' . $suffix . '.js', array('jquery'), EDD_VERSION);
            wp_enqueue_script('creditCardValidator');
        }
        wp_register_script('edd-checkout-global', $js_dir . 'edd-checkout-global' . $suffix . '.js', array('jquery'), EDD_VERSION);
        wp_enqueue_script('edd-checkout-global');
        wp_localize_script('edd-checkout-global', 'edd_global_vars', apply_filters('edd_global_checkout_script_vars', array('ajaxurl' => edd_get_ajax_url(), 'checkout_nonce' => wp_create_nonce('edd_checkout_nonce'), 'currency_sign' => edd_currency_filter(''), 'currency_pos' => edd_get_option('currency_position', 'before'), 'no_gateway' => __('Please select a payment method', 'edd'), 'no_discount' => __('Please enter a discount code', 'edd'), 'enter_discount' => __('Enter discount', 'edd'), 'discount_applied' => __('Discount Applied', 'edd'), 'no_email' => __('Please enter an email address before applying a discount code', 'edd'), 'no_username' => __('Please enter a username before applying a discount code', 'edd'), 'purchase_loading' => __('Please Wait...', 'edd'), 'complete_purchase' => __('Purchase', 'edd'), 'taxes_enabled' => edd_use_taxes() ? '1' : '0', 'edd_version' => EDD_VERSION)));
    }
    // Load AJAX scripts, if enabled
    if (!edd_is_ajax_disabled()) {
        wp_register_script('edd-ajax', $js_dir . 'edd-ajax' . $suffix . '.js', array('jquery'), EDD_VERSION);
        wp_enqueue_script('edd-ajax');
        wp_localize_script('edd-ajax', 'edd_scripts', apply_filters('edd_ajax_script_vars', array('ajaxurl' => edd_get_ajax_url(), 'position_in_cart' => isset($position) ? $position : -1, 'already_in_cart_message' => __('You have already added this item to your cart', 'edd'), 'empty_cart_message' => __('Your cart is empty', 'edd'), 'loading' => __('Loading', 'edd'), 'select_option' => __('Please select an option', 'edd'), 'ajax_loader' => set_url_scheme(EDD_PLUGIN_URL . 'assets/images/loading.gif', 'relative'), 'is_checkout' => edd_is_checkout() ? '1' : '0', 'default_gateway' => edd_get_default_gateway(), 'redirect_to_checkout' => edd_straight_to_checkout() || edd_is_checkout() ? '1' : '0', 'checkout_page' => edd_get_checkout_uri(), 'permalinks' => get_option('permalink_structure') ? '1' : '0', 'quantities_enabled' => edd_item_quantities_enabled(), 'taxes_enabled' => edd_use_taxes() ? '1' : '0')));
    }
}
/**
 * Load Scripts
 *
 * Enqueues the required scripts.
 *
 * @access      private
 * @since       1.0
 * @return      void
*/
function edd_load_scripts()
{
    global $edd_options, $post;
    wp_enqueue_script('jquery');
    // Get position in cart of current download
    if (isset($post->ID)) {
        $position = edd_get_item_position_in_cart($post->ID);
    }
    // Load AJAX scripts, if enabled
    if (edd_is_ajax_enabled()) {
        wp_enqueue_script('edd-ajax', EDD_PLUGIN_URL . 'assets/js/edd-ajax.js', array('jquery'), EDD_VERSION);
        wp_localize_script('edd-ajax', 'edd_scripts', array('ajaxurl' => edd_get_ajax_url(), 'ajax_nonce' => wp_create_nonce('edd_ajax_nonce'), 'no_discount' => __('Please enter a discount code', 'edd'), 'discount_applied' => __('Discount Applied', 'edd'), 'no_email' => __('Please enter an email address before applying a discount code', 'edd'), 'no_username' => __('Please enter a username before applying a discount code', 'edd'), 'position_in_cart' => isset($position) ? $position : -1, 'already_in_cart_message' => __('You have already added this item to your cart', 'edd'), 'empty_cart_message' => __('Your cart is empty', 'edd'), 'loading' => __('Loading', 'edd'), 'ajax_loader' => EDD_PLUGIN_URL . 'assets/images/loading.gif', 'checkout_page' => edd_get_checkout_uri(), 'permalinks' => get_option('permalink_structure') ? '1' : '0'));
    }
    // Load jQuery validation
    if (isset($edd_options['jquery_validation']) && edd_is_checkout()) {
        wp_enqueue_script('jquery-validation', EDD_PLUGIN_URL . 'assets/js/jquery.validate.min.js');
        wp_enqueue_script('edd-validation', EDD_PLUGIN_URL . 'assets/js/form-validation.js', array('jquery', 'jquery-validation'), EDD_VERSION);
        $required = array('firstname' => true, 'lastname' => true);
        wp_localize_script('edd-validation', 'edd_scripts_validation', apply_filters('edd_scripts_validation', $required));
    }
    if (edd_is_checkout()) {
        if (edd_is_cc_verify_enabled()) {
            wp_enqueue_script('creditCardValidator', EDD_PLUGIN_URL . 'assets/js/jquery.creditCardValidator.js', array('jquery'), EDD_VERSION);
        }
        wp_enqueue_script('edd-checkout-global', EDD_PLUGIN_URL . 'assets/js/edd-checkout-global.js', array('jquery'), EDD_VERSION);
        wp_localize_script('edd-checkout-global', 'edd_global_vars', array('currency_sign' => edd_currency_filter(''), 'currency_pos' => isset($edd_options['currency_position']) ? $edd_options['currency_position'] : 'before', 'no_gateway' => __('Please select a payment method', 'edd')));
    }
}
 /**
  * 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();
 }
/**
 * Load Scripts
 *
 * Enqueues the required scripts.
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_load_scripts()
{
    global $edd_options, $post;
    wp_enqueue_script('jquery');
    // Get position in cart of current download
    if (isset($post->ID)) {
        $position = edd_get_item_position_in_cart($post->ID);
    }
    // Load AJAX scripts, if enabled
    if (edd_is_ajax_enabled()) {
        wp_enqueue_script('edd-ajax', EDD_PLUGIN_URL . 'includes/js/edd-ajax.js');
        wp_localize_script('edd-ajax', 'edd_scripts', array('ajaxurl' => admin_url('admin-ajax.php'), 'ajax_nonce' => wp_create_nonce('edd_ajax_nonce'), 'no_discount' => __('Please enter a discount code', 'edd'), 'discount_applied' => __('Discount Applied', 'edd'), 'no_email' => __('Please enter an email address before applying a discount code', 'edd'), 'position_in_cart' => isset($position) ? $position : -1, 'already_in_cart_message' => __('You have already added this item to your cart', 'edd'), 'empty_cart_message' => __('Your cart is empty', 'edd'), 'loading' => __('Loading', 'edd'), 'ajax_loader' => EDD_PLUGIN_URL . 'includes/images/loading.gif', 'checkout_page' => isset($edd_options['purchase_page']) ? get_permalink($edd_options['purchase_page']) : '', 'permalinks' => get_option('permalink_structure') ? '1' : '0'));
    }
    // Load jQuery validation
    if (isset($edd_options['jquery_validation']) && is_page($edd_options['purchase_page'])) {
        wp_enqueue_script('jquery-validation', EDD_PLUGIN_URL . 'includes/js/jquery.validate.min.js');
        wp_enqueue_script('edd-validation', EDD_PLUGIN_URL . 'includes/js/form-validation.js');
        $required = array('firstname' => true, 'lastname' => true);
        wp_localize_script('edd-validation', 'edd_scripts_validation', apply_filters('edd_scripts_validation', $required));
    }
    wp_enqueue_script('edd-checkout-global', EDD_PLUGIN_URL . 'includes/js/edd-checkout-global.js');
}
/**
 * Get Cart Item Quantity
 *
 * @since 1.0
 * @param int $download_id Download (cart item) ID number
 * @param array $options Download options, such as price ID
 * @return int $quantity Cart item quantity
 */
function edd_get_cart_item_quantity($download_id = 0, $options = array())
{
    $cart = edd_get_cart_contents();
    $key = edd_get_item_position_in_cart($download_id, $options);
    $quantity = isset($cart[$key]['quantity']) && edd_item_quantities_enabled() ? $cart[$key]['quantity'] : 1;
    if ($quantity < 1) {
        $quantity = 1;
    }
    return apply_filters('edd_get_cart_item_quantity', $quantity, $download_id, $options);
}
Example #6
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;
}
/**
 * Retrieves the total discounted amount on the cart
 *
 * @since 1.4.1
 * @param array $discounts Discount codes
 * @return float $discounted_amount Total discounted amount
 */
function edd_get_cart_discounted_amount($discounts = false)
{
    if (empty($discounts)) {
        $discounts = edd_get_cart_discounts();
    }
    // Setup the array of discounts
    if (!empty($_POST['edd-discount']) && empty($discounts)) {
        // Check for a posted discount
        $posted_discount = isset($_POST['edd-discount']) ? trim($_POST['edd-discount']) : false;
        if ($posted_discount) {
            $discounts = array();
            $discounts[] = $posted_discount;
        }
    }
    // Return 0.00 if no discounts present
    if (empty($discounts)) {
        return 0.0;
    }
    $subtotal = edd_get_cart_subtotal($tax = false);
    $amounts = array();
    $discounted_items = array();
    foreach ($discounts as $discount) {
        $code_id = edd_get_discount_id_by_code($discount);
        $reqs = edd_get_discount_product_reqs($code_id);
        // Make sure requirements are set and that this discount shouldn't apply to the whole cart
        if (!empty($reqs) && edd_is_discount_not_global($code_id)) {
            // This is a product(s) specific discount
            $condition = edd_get_discount_product_condition($code_id);
            $cart_items = edd_get_cart_contents();
            foreach ($reqs as $download_id) {
                if (edd_item_in_cart($download_id)) {
                    $cart_key = edd_get_item_position_in_cart($download_id);
                    $price = edd_get_cart_item_price($download_id, $cart_items[$cart_key]['options']);
                    $amount = edd_get_discounted_amount($discount, $price);
                    $discounted_items[] = $price - $amount;
                }
            }
        } else {
            // This is a global cart discount
            $subtotal = edd_get_cart_subtotal();
            $amount = edd_get_discounted_amount($discount, $subtotal);
            $amounts[] = $subtotal - $amount;
        }
    }
    // Add up the total amount
    $discounted_amount = 0.0;
    $item_discount = array_sum($discounted_items);
    $global_discount = array_sum($amounts);
    $discounted_amount += $item_discount;
    $discounted_amount += $global_discount;
    return apply_filters('edd_get_cart_discounted_amount', edd_sanitize_amount($discounted_amount));
}
Example #8
0
/**
 * @since 3.0.2
 * @param $discount float The current discount amount on the item in the cart
 * @param $item array the cart item array
 * @return float
 */
function edd_sl_cart_details_item_discount($discount, $item)
{
    if (!edd_sl_renewals_allowed()) {
        return $discount;
    }
    if (!EDD()->session->get('edd_is_renewal')) {
        return $discount;
    }
    $renewal_keys = edd_sl_get_renewal_keys();
    $key = false;
    $options = isset($item['options']) ? $item['options'] : array();
    $cart_key = edd_get_item_position_in_cart($item['id'], $options);
    if (isset($renewal_keys[$cart_key])) {
        $key = $renewal_keys[$cart_key];
    }
    if (!$key) {
        return $discount;
    }
    $discount += edd_sl_get_renewal_discount_amount($item, $key);
    return $discount;
}