/** * Process the Add to Cart request * * @since 1.0 * * @param $data */ function edd_process_add_to_cart($data) { $download_id = absint($data['download_id']); $options = isset($data['edd_options']) ? $data['edd_options'] : array(); if (!empty($data['edd_download_quantity'])) { $options['quantity'] = absint($data['edd_download_quantity']); } if (isset($options['price_id']) && is_array($options['price_id'])) { foreach ($options['price_id'] as $key => $price_id) { $options['quantity'][$key] = isset($data['edd_download_quantity_' . $price_id]) ? absint($data['edd_download_quantity_' . $price_id]) : 1; } } $cart = edd_add_to_cart($download_id, $options); if (edd_straight_to_checkout() && !edd_is_checkout()) { $query_args = remove_query_arg(array('edd_action', 'download_id', 'edd_options')); $query_part = strpos($query_args, "?"); $url_parameters = ''; if (false !== $query_part) { $url_parameters = substr($query_args, $query_part); } wp_redirect(edd_get_checkout_uri() . $url_parameters, 303); edd_die(); } else { wp_redirect(remove_query_arg(array('edd_action', 'download_id', 'edd_options'))); edd_die(); } }
/** * Adds item to the cart via AJAX. * * @since 1.0 * @return void */ function edd_ajax_add_to_cart() { if (isset($_POST['download_id']) && check_ajax_referer('edd_ajax_nonce', 'nonce')) { global $post; $to_add = array(); if (isset($_POST['price_ids']) && is_array($_POST['price_ids'])) { foreach ($_POST['price_ids'] as $price) { $to_add[] = array('price_id' => $price); } } foreach ($to_add as $options) { if (!edd_item_in_cart($_POST['download_id'], $options)) { if ($_POST['download_id'] == $options['price_id']) { $options = array(); } $key = edd_add_to_cart($_POST['download_id'], $options); $item = array('id' => $_POST['download_id'], 'options' => $options); $item = apply_filters('edd_ajax_pre_cart_item_template', $item); $cart_item = edd_get_cart_item_template($key, $item, true); echo $cart_item; } else { echo 'incart'; } } } edd_die(); }
/** * Adds item to the cart from the wish list via AJAX. Based off edd_ajax_add_to_cart() * * @since 1.0 * @return void */ function edd_ajax_add_to_cart_from_wish_list() { if (isset($_POST['download_id'])) { global $post; $to_add = array(); if (isset($_POST['price_ids']) && is_array($_POST['price_ids'])) { foreach ($_POST['price_ids'] as $price) { $to_add[] = array('price_id' => $price); } } foreach ($to_add as $options) { if ($_POST['download_id'] == $options['price_id']) { $options = array(); } parse_str($_POST['post_data'], $post_data); if (isset($options['price_id']) && isset($post_data['edd_download_quantity_' . $options['price_id']])) { $options['quantity'] = absint($post_data['edd_download_quantity_' . $options['price_id']]); } else { $options['quantity'] = isset($post_data['edd_download_quantity']) ? absint($post_data['edd_download_quantity']) : 1; } // call EDD's edd_add_to_cart function $key = edd_add_to_cart($_POST['download_id'], $options); $item = array('id' => $_POST['download_id'], 'options' => $options); $item = apply_filters('edd_wl_ajax_pre_cart_item_template', $item); $return = array('add_to_cart' => 'Added to Cart', 'subtotal' => html_entity_decode(edd_currency_filter(edd_format_amount(edd_get_cart_subtotal())), ENT_COMPAT, 'UTF-8'), 'cart_item' => html_entity_decode(edd_get_cart_item_template($key, $item, true), ENT_COMPAT, 'UTF-8')); echo json_encode($return); } } edd_die(); }
/** * Process the Add to Cart request * * @since 1.0 * @return void */ function edd_process_add_to_cart($data) { $download_id = $data['download_id']; $options = isset($data['edd_options']) ? $data['edd_options'] : array(); $cart = edd_add_to_cart($download_id, $options); if (edd_straight_to_checkout() && !edd_is_checkout()) { wp_redirect(edd_get_checkout_uri(), 303); edd_die(); } }
/** * Process the Add to Cart request * * @since 1.0 * * @param $data */ function edd_process_add_to_cart( $data ) { $download_id = absint( $data['download_id'] ); $options = isset( $data['edd_options'] ) ? $data['edd_options'] : array(); $cart = edd_add_to_cart( $download_id, $options ); if ( edd_straight_to_checkout() && ! edd_is_checkout() ) { wp_redirect( edd_get_checkout_uri(), 303 ); edd_die(); } else { wp_redirect( remove_query_arg( array( 'edd_action', 'download_id', 'edd_options' ) ) ); edd_die(); } }
/** * 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(); }
/** * AJAX Add To Cart * * Adds item to the cart. * * @access private * @since 1.0 * @return string */ function edd_ajax_add_to_cart() { if (isset($_POST['download_id']) && check_ajax_referer('edd_ajax_nonce', 'nonce')) { global $post; if (!edd_item_in_cart($_POST['download_id'])) { $options = is_numeric($_POST['price_id']) ? array('price_id' => $_POST['price_id']) : array(); $key = edd_add_to_cart($_POST['download_id'], $options); $item = array('id' => $_POST['download_id'], 'options' => $options); $cart_item = edd_get_cart_item_template($key, $item, true); echo $cart_item; } else { echo 'incart'; } } die; }
/** * Process the Add to Cart request * * @since 1.0 * * @param $data */ function edd_process_add_to_cart($data) { $download_id = absint($data['download_id']); $options = isset($data['edd_options']) ? $data['edd_options'] : array(); if (!empty($data['edd_download_quantity'])) { $options['quantity'] = absint($data['edd_download_quantity']); } if (is_array($options['price_id'])) { foreach ($options['price_id'] as $key => $price_id) { $options['quantity'][$key] = absint($data['edd_download_quantity_' . $price_id]); } } $cart = edd_add_to_cart($download_id, $options); if (edd_straight_to_checkout() && !edd_is_checkout()) { wp_redirect(edd_get_checkout_uri(), 303); edd_die(); } else { wp_redirect(remove_query_arg(array('edd_action', 'download_id', 'edd_options'))); edd_die(); } }
/** * Adds item to the cart via AJAX. * * @since 1.0 * @return void */ function edd_ajax_add_to_cart() { if (isset($_POST['download_id'])) { $to_add = array(); if (isset($_POST['price_ids']) && is_array($_POST['price_ids'])) { foreach ($_POST['price_ids'] as $price) { $to_add[] = array('price_id' => $price); } } $items = ''; foreach ($to_add as $options) { if ($_POST['download_id'] == $options['price_id']) { $options = array(); } parse_str($_POST['post_data'], $post_data); if (isset($options['price_id']) && isset($post_data['edd_download_quantity_' . $options['price_id']])) { $options['quantity'] = absint($post_data['edd_download_quantity_' . $options['price_id']]); } else { $options['quantity'] = isset($post_data['edd_download_quantity']) ? absint($post_data['edd_download_quantity']) : 1; } $key = edd_add_to_cart($_POST['download_id'], $options); $item = array('id' => $_POST['download_id'], 'options' => $options); $item = apply_filters('edd_ajax_pre_cart_item_template', $item); $items .= html_entity_decode(edd_get_cart_item_template($key, $item, true), ENT_COMPAT, 'UTF-8'); } $return = array('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_item' => $items, '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(); }
/** * Process Add To Cart * * Process the add to cart request. * * @access private * @since 1.0 * @return void */ function edd_process_add_to_cart($data) { $download_id = $data['download_id']; $options = isset($data['edd_options']) ? $data['edd_options'] : array(); $cart = edd_add_to_cart($download_id, $options); }
/** * Add license upgrade to the cart * * @since 3.3 * @return void */ function edd_sl_add_upgrade_to_cart($data) { if (!is_user_logged_in()) { return; } if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'edd_sl_upgrade_nonce')) { return; } $download_id = edd_software_licensing()->get_download_id($data['license_id']); $cart_contents = edd_get_cart_contents(); $allow_upgrade = true; if (is_array($cart_contents)) { foreach ($cart_contents as $item) { if ($item['id'] == $download_id && !empty($item['options']['is_renewal'])) { $allow_upgrade = false; break; } } } if ('expired' === edd_software_licensing()->get_license_status($data['license_id'])) { $is_expired = true; $allow_upgrade = false; } if ($allow_upgrade) { $upgrade = edd_sl_get_upgrade_path($download_id, $data['upgrade_id']); $options = array('price_id' => $upgrade['price_id'], 'is_upgrade' => true, 'upgrade_id' => $data['upgrade_id'], 'license_id' => $data['license_id'], 'cost' => edd_sl_get_license_upgrade_cost($data['license_id'], $data['upgrade_id'])); edd_add_to_cart($upgrade['download_id'], $options); } else { if (!empty($is_expired)) { edd_set_error('edd-sl-expired-license', __('Your license key is expired. It must be renewed before it can be upgraded.', 'edd_sl')); } else { edd_set_error('edd-sl-unique-action', __('Expired license keys cannot be upgraded. Renew your license and then you may upgrade it.', 'edd_sl')); } } wp_redirect(edd_get_checkout_uri()); exit; }
/** * Add Collection to Cart * * Adds all downloads within a taxonomy term to the cart. * * @access public * @since 1.0.6 * @param $taxonomy string - the name of the taxonomy * @param $terms mixed - the slug or id of the term from which to add ites, or an array of terms * @return array of IDs for each item added to the cart */ function edd_add_collection_to_cart($taxonomy, $terms) { if (!is_string($taxonomy)) { return false; } $field = is_int($terms) ? 'id' : 'slug'; $cart_item_ids = array(); $args = array('post_type' => 'download', 'posts_per_page' => -1, $taxonomy => $terms); $items = get_posts($args); if ($items) { foreach ($items as $item) { edd_add_to_cart($item->ID); $cart_item_ids[] = $item->ID; } } return $cart_item_ids; }
/** * Handles the process of adding a ticket product to the cart. * * If the cart already contains a line item for the same product, simply increment the * quantity for that item accordingly. * * @see bug #28917 * @param $product_id * @param $quantity */ protected function add_ticket_to_cart($product_id, $quantity) { // Is the item in the cart already? Simply adjust the quantity if so if (edd_item_in_cart($product_id)) { $existing_quantity = edd_get_cart_item_quantity($product_id); $quantity += $existing_quantity; edd_set_cart_item_quantity($product_id, $quantity); } else { $options = array('quantity' => $quantity); edd_add_to_cart($product_id, $options); } }
/** * 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')); }
/** * Add all items in wish list to the cart * * Adds all downloads within a taxonomy term to the cart. * * @since 1.0.6 * @param int $list_id ID of the list * @return array Array of IDs for each item added to the cart */ function edd_wl_add_all_to_cart($list_id) { $cart_item_ids = array(); $items = edd_wl_get_wish_list($list_id); if ($items) { foreach ($items as $item) { // check that they aren't already in the cart if (edd_item_in_cart($item['id'], $item['options'])) { continue; } edd_add_to_cart($item['id'], $item['options']); $cart_item_ids[] = $item['id']; } } }
/** * Add the license upgrade from trial to the cart * * @param int $license_id * * @return string */ protected static function prepare_upgrade($license_id) { $download_id = \EDD_Software_Licensing::instance()->get_download_id($license_id); if (is_numeric($download_id)) { $upgrades = edd_sl_get_upgrade_paths($download_id); if (is_array($upgrades) && !empty($upgrades)) { reset($upgrades); $upgrade_price_id = key($upgrades); $upgrade = edd_sl_get_upgrade_path(ids::$trial_id, $upgrade_price_id); $options = array('price_id' => $upgrade['price_id'], 'is_upgrade' => true, 'upgrade_id' => $upgrade_price_id, 'license_id' => $license_id, 'cost' => edd_sl_get_license_upgrade_cost($license_id, $upgrade_price_id)); $added = edd_add_to_cart($upgrade['download_id'], $options); return $added; } } }
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; }
/** * Track EDD at to cart conversions * * @since 1.1.0 * * @group group * @group destination * @group edd * * @covers \ingot\testing\tests\click\destination\hooks::edd_post_add_to_cart() * @covers \ingot\testing\tests\click\destination\hooks::add_hooks() */ public function testEDDConversionsCart() { if (ingot_is_edd_active()) { $this->assertTrue(\ingot\testing\tests\click\destination\types::allowed_destination_type('cart_edd')); $product = ingot_test_data_price::edd_create_simple_download(10); $data = ingot_test_desitnation::create('cart_edd'); $group_id = $data['group_ID']; $variants = $data['variants']; $this->assertInternalType('array', $variants); $this->assertFalse(empty($variants)); new \ingot\testing\bandit\content($group_id); $hooks = new \ingot\testing\tests\click\destination\hooks([$group_id]); $hooks->edd_post_add_to_cart(); $group = \ingot\testing\crud\group::read($group_id); $totals = \ingot\testing\utility\group::get_total($group, true); $this->assertEquals(1, $totals['total']); $this->assertEquals(1, $totals['conversions']); edd_add_to_cart($product->ID); $group = \ingot\testing\crud\group::read($group_id); $totals = \ingot\testing\utility\group::get_total($group, true); $this->assertEquals(3, $totals['total']); $this->assertEquals(2, $totals['conversions']); } }
/** * Adds item to the cart via AJAX. * * @since 1.0 * @return void */ function edd_ajax_add_to_cart() { if (isset($_POST['download_id'])) { $to_add = array(); if (isset($_POST['price_ids']) && is_array($_POST['price_ids'])) { foreach ($_POST['price_ids'] as $price) { $to_add[] = array('price_id' => $price); } } $items = ''; foreach ($to_add as $options) { if ($_POST['download_id'] == $options['price_id']) { $options = array(); } $key = edd_add_to_cart($_POST['download_id'], $options); $item = array('id' => $_POST['download_id'], 'options' => $options); $item = apply_filters('edd_ajax_pre_cart_item_template', $item); $items .= html_entity_decode(edd_get_cart_item_template($key, $item, true), ENT_COMPAT, 'UTF-8'); } $return = array('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_item' => $items); echo json_encode($return); } edd_die(); }
/** * Add Collection to Cart * * Adds all downloads within a taxonomy term to the cart. * * @since 1.0.6 * @param string $taxonomy Name of the taxonomy * @param mixed $terms Slug or ID of the term from which to add ites | An array of terms * @return array Array of IDs for each item added to the cart */ function edd_add_collection_to_cart($taxonomy, $terms) { if (!is_string($taxonomy)) { return false; } if (is_numeric($terms)) { $terms = get_term($terms, $taxonomy); $terms = $terms->slug; } $cart_item_ids = array(); $args = array('post_type' => 'download', 'posts_per_page' => -1, $taxonomy => $terms); $items = get_posts($args); if ($items) { foreach ($items as $item) { edd_add_to_cart($item->ID); $cart_item_ids[] = $item->ID; } } return $cart_item_ids; }
/** * Processes the license upgrade */ function affwp_process_license_upgrade() { // get type. plus, professional or ultimate $type = isset($_GET['type']) ? strtolower($_GET['type']) : ''; $license = isset($_GET['key']) ? strtolower($_GET['key']) : ''; if (!is_user_logged_in() || 'plus' !== $type && 'professional' !== $type && 'ultimate' !== $type) { // Isn't logged in, so go back to pricing wp_redirect(home_url('/pricing')); exit; } $affwp_id = affwp_get_affiliatewp_id(); $licenses = affwp_get_users_licenses(); $has_ultimate_license = in_array(3, affwp_get_users_price_ids()); $has_professional_license = in_array(2, affwp_get_users_price_ids()); $has_plus_license = in_array(1, affwp_get_users_price_ids()); $has_personal_license = in_array(0, affwp_get_users_price_ids()); switch ($type) { case 'ultimate': if ($has_professional_license) { $discount = 199; } elseif ($has_plus_license) { $discount = 99; } elseif ($has_personal_license) { $discount = 49; } else { // Hasn't purchased, so go back to pricing wp_redirect(home_url('/pricing')); exit; } $price_id = 3; break; case 'professional': if ($has_plus_license) { $discount = 99; } elseif ($has_personal_license) { $discount = 49; } else { // Hasn't purchased, so go back to pricing wp_redirect(home_url('/pricing')); exit; } $price_id = 2; break; case 'plus': if ($has_personal_license) { $discount = 49; } else { // Hasn't purchased, so go back to pricing wp_redirect(home_url('/pricing')); exit; } $price_id = 1; break; } // end switch // Remove anything in the cart edd_empty_cart(); // Add the correct license edd_add_to_cart($affwp_id, array('price_id' => $price_id, 'upgrade' => $license)); EDD()->session->set('is_upgrade', '1'); EDD()->session->set('upgrade_price_id', $price_id); EDD()->session->set('upgrade_key', $license); EDD()->session->set('upgrade_discount', $discount); wp_redirect(edd_get_checkout_uri()); exit; }