/** * Get Purchase Link * * Builds a Purchase link for a specified download based on arguments passed. * This function is used all over EDD to generate the Purchase or Add to Cart * buttons. If no arguments are passed, the function uses the defaults that have * been set by the plugin. The Purchase link is built for simple and variable * pricing and filters are available throughout the function to override * certain elements of the function. * * $download_id = null, $link_text = null, $style = null, $color = null, $class = null * * @since 1.0 * @param array $args Arguments for display * @return string $purchase_form */ function edd_get_purchase_link($args = array()) { global $edd_options, $post; if (!isset($edd_options['purchase_page']) || $edd_options['purchase_page'] == 0) { edd_set_error('set_checkout', sprintf(__('No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'edd'), admin_url('edit.php?post_type=download&page=edd-settings'))); edd_print_errors(); return false; } $post_id = is_object($post) ? $post->ID : 0; $defaults = apply_filters('edd_purchase_link_defaults', array('download_id' => $post_id, 'price' => (bool) true, 'direct' => edd_get_download_button_behavior($post_id) == 'direct' ? true : false, 'text' => !empty($edd_options['add_to_cart_text']) ? $edd_options['add_to_cart_text'] : __('Purchase', 'edd'), 'style' => isset($edd_options['button_style']) ? $edd_options['button_style'] : 'button', 'color' => isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'blue', 'class' => 'edd-submit')); $args = wp_parse_args($args, $defaults); if ('publish' != get_post_field('post_status', $args['download_id']) && !current_user_can('edit_product', $args['download_id'])) { return false; // Product not published or user doesn't have permission to view drafts } // Override color if color == inherit $args['color'] = $args['color'] == 'inherit' ? '' : $args['color']; $variable_pricing = edd_has_variable_prices($args['download_id']); $data_variable = $variable_pricing ? ' data-variable-price="yes"' : 'data-variable-price="no"'; $type = edd_single_price_option_mode($args['download_id']) ? 'data-price-mode=multi' : 'data-price-mode=single'; if ($args['price'] && $args['price'] !== 'no' && !$variable_pricing) { $price = edd_get_download_price($args['download_id']); $button_text = !empty($args['text']) ? ' – ' . $args['text'] : ''; if (0 == $price) { $args['text'] = __('Free', 'edd') . $button_text; } else { $args['text'] = edd_currency_filter(edd_format_amount($price)) . $button_text; } } if (edd_item_in_cart($args['download_id']) && !$variable_pricing) { $button_display = 'style="display:none;"'; $checkout_display = ''; } else { $button_display = ''; $checkout_display = 'style="display:none;"'; } global $edd_displayed_form_ids; $form_id = !empty($args['form_id']) ? $args['form_id'] : 'edd_purchase_' . $args['download_id']; $args = apply_filters('edd_purchase_link_args', $args); ob_start(); ?> <form id="<?php echo $form_id; ?> " class="edd_download_purchase_form" method="post"> <?php do_action('edd_purchase_link_top', $args['download_id'], $args); ?> <div class="edd_purchase_submit_wrapper"> <?php $class = implode(' ', array($args['style'], $args['color'], trim($args['class']))); if (!edd_is_ajax_disabled()) { echo '<a href="#" class="edd-add-to-cart ' . esc_attr($class) . '" data-action="edd_add_to_cart" data-download-id="' . esc_attr($args['download_id']) . '" ' . $data_variable . ' ' . $type . ' ' . $button_display . '><span class="edd-add-to-cart-label">' . $args['text'] . '</span> <span class="edd-loading"><i class="edd-icon-spinner edd-icon-spin"></i></span></a>'; } echo '<input type="submit" class="edd-add-to-cart edd-no-js ' . esc_attr($class) . '" name="edd_purchase_download" value="' . esc_attr($args['text']) . '" data-action="edd_add_to_cart" data-download-id="' . esc_attr($args['download_id']) . '" ' . $data_variable . ' ' . $type . ' ' . $button_display . '/>'; echo '<a href="' . esc_url(edd_get_checkout_uri()) . '" class="edd_go_to_checkout ' . esc_attr($class) . '" ' . $checkout_display . '>' . __('Checkout', 'edd') . '</a>'; ?> <?php if (!edd_is_ajax_disabled()) { ?> <span class="edd-cart-ajax-alert"> <span class="edd-cart-added-alert" style="display: none;"> <?php printf(__('<i class="edd-icon-ok"></i> Added to cart', 'edd'), '<a href="' . esc_url(edd_get_checkout_uri()) . '" title="' . __('Go to Checkout', 'edd') . '">', '</a>'); ?> </span> </span> <?php } ?> <?php if (edd_display_tax_rate() && edd_prices_include_tax()) { echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Includes %1$s% tax', 'edd'), edd_get_tax_rate() * 100) . '</span>'; } elseif (edd_display_tax_rate() && !edd_prices_include_tax()) { echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Excluding %1$s% tax', 'edd'), edd_get_tax_rate() * 100) . '</span>'; } ?> </div><!--end .edd_purchase_submit_wrapper--> <input type="hidden" name="download_id" value="<?php echo esc_attr($args['download_id']); ?> "> <?php if (!empty($args['direct'])) { ?> <input type="hidden" name="edd_action" class="edd_action_input" value="straight_to_gateway"> <?php } else { ?> <input type="hidden" name="edd_action" class="edd_action_input" value="add_to_cart"> <?php } ?> <?php do_action('edd_purchase_link_end', $args['download_id'], $args); ?> </form><!--end #<?php echo esc_attr($form_id); ?> --> <?php $purchase_form = ob_get_clean(); return apply_filters('edd_purchase_download_form', $purchase_form, $args); }
/** * 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(); }
/** * Add To Cart * * Adds a download ID to the shopping cart. * Uses edd_get_cart_contents(). * * @access public * @since 1.0 * @param $download_id - INT the ID number of the download to add to the cart * @param $options - array an array of options, such as variable price * @return string - cart key of the new item */ function edd_add_to_cart($download_id, $options = array()) { $cart = edd_get_cart_contents(); if (!edd_item_in_cart($download_id)) { if ('download' != get_post_type($download_id)) { return; } // not a download product do_action('edd_pre_add_to_cart', $download_id, $options); if (edd_has_variable_prices($download_id) && !isset($options['price_id'])) { // forces to the first price ID if none is specified and download has variable prices $options['price_id'] = 0; } $cart_item = apply_filters('edd_add_to_cart_item', array('id' => $download_id, 'options' => $options)); if (is_array($cart)) { $cart[] = $cart_item; } else { $cart = array($cart_item); } $_SESSION['edd_cart'] = $cart; do_action('edd_post_add_to_cart', $download_id, $options); // clear all the checkout errors, if any edd_clear_errors(); return count($cart) - 1; } }
function pw_edd_disable_gateway_on_checkout($gateways) { if (edd_is_checkout()) { // Disable Stripe if the download with an ID of 10 is in the cart if (edd_item_in_cart(10)) { unset($gateways['stripe']); } } return $gateways; }
/** * Prevents items from being added to the cart multiple times * */ function pw_edd_prevent_duplicate_cart_items($download_id, $options) { if (edd_item_in_cart($download_id, $options)) { if (edd_is_ajax_enabled()) { die('1'); } else { wp_redirect(edd_get_checkout_uri()); exit; } } }
/** * 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; }
/** * Add To Cart * * Adds a download ID to the shopping cart. * Uses edd_get_cart_contents(). * * @access public * @since 1.0 * @param $download_id - INT the ID number of the download to add to the cart * @param $options - array an array of options, such as variable price * @return string - cart key of the new item */ function edd_add_to_cart($download_id, $options = array()) { $cart = edd_get_cart_contents(); if (!edd_item_in_cart($download_id)) { do_action('edd_pre_add_to_cart', $download_id, $options); if (is_array($cart)) { $cart[] = array('id' => $download_id, 'options' => $options); } else { $cart = array(array('id' => $download_id, 'options' => $options)); } $_SESSION['edd_cart'] = $cart; do_action('edd_post_add_to_cart', $download_id, $options); // clear all the checkout errors, if any edd_clear_errors(); return count($cart) - 1; } }
/** * Add To Cart * * Adds a download ID to the shopping cart. * * @since 1.0 * * @param int $download_id Download IDs to be added to the cart * @param array $options Array of options, such as variable price * * @return string Cart key of the new item */ function edd_add_to_cart($download_id, $options = array()) { $cart = edd_get_cart_contents(); if (!edd_item_in_cart($download_id, $options)) { $download = get_post($download_id); if ('download' != $download->post_type) { return; } // Not a download product if (!current_user_can('edit_post', $download->ID) && ($download->post_status == 'draft' || $download->post_status == 'pending')) { return; } // Do not allow draft/pending to be purchased if can't edit. Fixes #1056 do_action('edd_pre_add_to_cart', $download_id, $options); if (edd_has_variable_prices($download_id) && !isset($options['price_id'])) { // Forces to the first price ID if none is specified and download has variable prices $options['price_id'] = 0; } $to_add = array(); if (isset($options['price_id']) && is_array($options['price_id'])) { // Process multiple price options at once foreach ($options['price_id'] as $price) { $price_options = array('price_id' => $price); $to_add[] = apply_filters('edd_add_to_cart_item', array('id' => $download_id, 'options' => $price_options)); } } else { // Add a single item $to_add[] = apply_filters('edd_add_to_cart_item', array('id' => $download_id, 'options' => $options)); } if (is_array($cart)) { $cart = array_merge($cart, $to_add); } else { $cart = $to_add; } EDD()->session->set('edd_cart', $cart); do_action('edd_post_add_to_cart', $download_id, $options); // Clear all the checkout errors, if any edd_clear_errors(); return count($cart) - 1; } }
/** * Get Purchase Link * * Returns the purchase link. * * @access public * @since 1.0 * @return string */ function edd_get_purchase_link($download_id = null, $link_text = null, $style = null, $color = null, $class = '') { global $edd_options, $post, $user_ID; if (!isset($edd_options['purchase_page']) || $edd_options['purchase_page'] == 0) { edd_set_error('set_checkout', sprintf(__('No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'edd'), admin_url('edit.php?post_type=download&page=edd-settings'))); edd_print_errors(); return false; } $page = get_permalink($post->ID); // current page $link_args = array('download_id' => $download_id, 'edd_action' => 'add_to_cart'); $link = add_query_arg($link_args, $page); $checkout_url = get_permalink($edd_options['purchase_page']); $variable_pricing = edd_has_variable_prices($download_id); if (is_null($link_text)) { $link_text = get_post_meta($post->ID, '_edd_purchase_text', true) ? get_post_meta($post->ID, '_edd_purchase_text', true) : __('Purchase', 'edd'); } if (is_null($style)) { $style = get_post_meta($post->ID, '_edd_purchase_style', true) ? get_post_meta($post->ID, '_edd_purchase_style', true) : 'button'; } if (is_null($color)) { $color = get_post_meta($post->ID, '_edd_purchase_color', true) ? str_replace(' ', '_', get_post_meta($post->ID, '_edd_purchase_color', true)) : 'blue'; } $purchase_form = '<form id="edd_purchase_' . $download_id . '" class="edd_download_purchase_form" method="POST">'; if ($variable_pricing) { $prices = edd_get_variable_prices($download_id); $purchase_form .= '<div class="edd_price_options">'; if ($prices) { foreach ($prices as $key => $price) { $checked = ''; if ($key == 0) { $checked = 'checked="checked"'; } $purchase_form .= sprintf('<input type="radio" %1$s name="edd_options[price_id]" id="%2$s" class="%3$s" value="%4$s"/> ', $checked, esc_attr('edd_price_option_' . $download_id . '_' . $key), esc_attr('edd_price_option_' . $download_id), esc_attr($key)); $purchase_form .= sprintf('<label for="%1$s">%2$s</label><br/>', esc_attr('edd_price_option_' . $download_id . '_' . $key), esc_html($price['name'] . ' - ' . edd_currency_filter($price['amount']))); } } $purchase_form .= '</div><!--end .edd_price_options-->'; } $purchase_form .= '<div class="edd_purchase_submit_wrapper">'; $data_variable = $variable_pricing ? ' data-variable-price="yes"' : ''; if (edd_item_in_cart($download_id)) { $button_display = 'style="display:none;"'; $checkout_display = ''; } else { $button_display = ''; $checkout_display = 'style="display:none;"'; } if ($style == 'button') { $purchase_button = sprintf('<span class="%1$s" %2$s>', esc_attr('edd_button edd_add_to_cart_wrap edd_' . $color), $button_display); $purchase_button .= '<span class="edd_button_outer">'; $purchase_button .= '<span class="edd_button_inner">'; $purchase_button .= sprintf('<input type="submit" class="%1$s" name="edd_purchase_download" value="%2$s" data-action="edd_add_to_cart" data-download-id="%3$s" %4$s/>', esc_attr('edd_button_text edd-submit edd-add-to-cart ' . $class), esc_attr($link_text), esc_attr($download_id), $data_variable); $purchase_button .= '</span>'; $purchase_button .= '</span>'; $purchase_button .= '</span>'; $checkout_link = sprintf('<a href="%1$s" class="%2$s" %3$s>', esc_url($checkout_url), esc_attr('edd_go_to_checkout edd_button edd_' . $color), $checkout_display); $checkout_link .= '<span class="edd_button_outer"><span class="edd_button_inner">'; $checkout_link .= '<span class="edd_button_text"><span>' . __('Checkout', 'edd') . '</span></span>'; $checkout_link .= '</span></span>'; $checkout_link .= '</a>'; $purchase_form .= $purchase_button . $checkout_link; } else { $purchase_text = sprintf('<input type="submit" class="%1$s" name="edd_purchase_download" value="%2$s" data-action="edd_add_to_cart" data-download-id="%3$s" %4$s %5$s/>', esc_attr('edd_submit_plain edd-add-to-cart ' . $class), esc_attr($link_text), esc_attr($download_id), esc_attr($data_variable), esc_attr($button_display)); $checkout_link = sprintf('<a href="%1$s" class="%2$s" %3$s>', esc_url($checkout_url), esc_attr('edd_go_to_checkout edd_button edd_' . $color), $checkout_display); $checkout_link .= __('Checkout', 'edd'); $checkout_link .= '</a>'; $purchase_form .= $purchase_text . $checkout_link; } if (edd_is_ajax_enabled()) { $purchase_form .= sprintf('<div class="edd-cart-ajax-alert"><img src="%1$s" class="edd-cart-ajax" style="display: none;"/>', esc_url(EDD_PLUGIN_URL . 'includes/images/loading.gif')); $purchase_form .= ' <span style="display:none;" class="edd-cart-added-alert">' . __('added to your cart', 'edd') . '</span></div>'; } $purchase_form .= '</div><!--end .edd_purchase_submit_wrapper-->'; $purchase_form .= '<input type="hidden" name="download_id" value="' . esc_attr($download_id) . '">'; $purchase_form .= '<input type="hidden" name="edd_action" value="add_to_cart">'; $purchase_form .= '</form><!--end #edd_purchase_' . esc_html($download_id) . '-->'; return apply_filters('edd_purchase_download_form', $purchase_form, $download_id, $link_text, $style, $color, $class); }
/** * Display price before product purchase form. * * @param int $download_id * @param array $args * @param string $price * @return void * @since 1.0.0 */ function reach_edd_show_price($download_id, $args, $price = null) { if (isset($args['price']) && 'no' === $args['price']) { return; } if (edd_has_variable_prices($download_id) || edd_item_in_cart($download_id)) { return; } if (is_null($price)) { $price = reach_get_edd_product_price($download_id, $args); } if (false === $price) { return; } ?> <div class="download-price"><?php echo edd_currency_filter(edd_format_amount($price)); ?> </div> <?php }
/** * Store extra meta information against the download at the time it is added to the cart * * @param $info the default array of meta information stored with the download * @return $info the new array of meta information * * @since 1.1 */ function edd_csau_add_to_cart_item($info) { // Cross-sells if (EDD()->session->get('edd_is_checkout')) { // if item is added from checkout page, and it's trigger product is already in the cart, then mark as cross-sell if (edd_item_in_cart(edd_csau_get_trigger_id($info['id'], 'cross_sell'))) { $info['cross_sell'] = true; } } else { // get the downloads trigger ID $trigger_id = edd_csau_get_trigger_id($info['id'], 'upsell'); // if download does not have a trigger ID, exit if (!$trigger_id) { return $info; } // use the trigger ID to get an array of upsell IDs $upsell_ids = get_post_meta($trigger_id, '_edd_csau_upsell_products'); // if this download exists in the upsell IDs array, then mark it as an upsell if (EDD()->session->get('edd_is_single') && in_array($info['id'], $upsell_ids)) { $info['upsell'] = true; } } return $info; }
/** * 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); } }
/** * Retrieve all active fees * * @access public * @since 1.5 * @param string $type Fee type, "fee" or "item" * @param int $download_id The download ID whose fees to retrieve * @uses EDD_Session::get() * @return mixed array|bool */ public function get_fees($type = 'fee', $download_id = 0) { $fees = EDD()->session->get('edd_cart_fees'); if (!edd_get_cart_contents()) { // We can only get item type fees when the cart is empty $type = 'item'; } if (!empty($fees) && !empty($type) && 'all' !== $type) { foreach ($fees as $key => $fee) { if (!empty($fee['type']) && $type != $fee['type']) { unset($fees[$key]); } } } if (!empty($fees) && !empty($download_id)) { // Remove fees that don't belong to the specified Download foreach ($fees as $key => $fee) { if ((int) $download_id !== (int) $fee['download_id']) { unset($fees[$key]); } } } if (!empty($fees)) { // Remove fees that belong to a specific download but are not in the cart foreach ($fees as $key => $fee) { if (empty($fee['download_id'])) { continue; } if (!edd_item_in_cart($fee['download_id'])) { unset($fees[$key]); } } } return !empty($fees) ? $fees : array(); }
<a href="<?php the_permalink(); ?> " title="<?php the_title(); ?> "><?php the_title(); ?> </a> </h3><!-- .entry-title --> </header><!-- .entry-header --> <div class="entry-meta"> <?php if (!edd_item_in_cart($post->ID)) { if (edd_has_variable_prices($post->ID)) { echo '<a href="' . get_permalink() . '" class="button edd_button">' . __('View Details', 'edd-digitalstore') . '</a>'; } else { echo do_shortcode('[purchase_link id="' . $post->ID . '" price="0" text="' . __('Add To Cart', 'edd-digitalstore') . '" style="blue"]'); } } else { echo '<a href="' . get_permalink($edd_options['purchase_page']) . '" class="edd_go_to_checkout edd_button">' . __('Checkout', 'edd-digitalstore') . '</a>'; } ?> <span class="entry-price"><?php echo digitalstore_edd_the_price($post->ID); ?> </span> </div><!-- .entry-meta -->
/** * Wish list item purchase link * * @since 1.0 * @param [type] $item [description] * @return [type] [description] */ function edd_wl_item_purchase($item, $args = array()) { global $edd_options; ob_start(); $defaults = apply_filters('edd_wl_add_to_cart_defaults', array('download_id' => $item['id'], 'text' => !empty($edd_options['edd_wl_add_to_cart']) ? $edd_options['edd_wl_add_to_cart'] : __('Add to cart', 'edd-wish-lists'), 'checkout_text' => __('Checkout', 'edd-wish-lists'), 'style' => edd_get_option('edd_wl_button_style', 'button'), 'color' => '', 'class' => 'edd-wl-action', 'wrapper' => 'span', 'wrapper_class' => '', 'link' => ''), $item['id']); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); $style = 'edd-wl-button'; $variable_pricing = edd_has_variable_prices($download_id); $data_variable = $variable_pricing ? ' data-variable-price=yes' : 'data-variable-price=no'; // price option $data_price_option = $variable_pricing ? ' data-price-option=' . $item['options']['price_id'] : ''; $type = edd_single_price_option_mode($download_id) ? 'data-price-mode=multi' : 'data-price-mode=single'; if (edd_item_in_cart($download_id) && !$variable_pricing) { // hide the 'add to cart' link $button_display = 'style="display:none;"'; // show the 'checkout' link $checkout_display = ''; } elseif ($variable_pricing && edd_item_in_cart($download_id, array('price_id' => $item['options']['price_id']))) { // hide the 'add to cart' link $button_display = 'style="display:none;"'; // show the 'checkout' link $checkout_display = ''; } else { // show the 'add to cart' link $button_display = ''; // hide the 'checkout' link $checkout_display = 'style="display:none;"'; } $button_size = ''; // if link is specified, don't show spinner $loading = !$link ? '<span class="edd-loading"><i class="edd-icon-spinner edd-icon-spin"></i></span>' : ''; $link = !$link ? '#' : $link; $form_id = !empty($form_id) ? $form_id : 'edd_purchase_' . $download_id; // add our default class $default_wrapper_class = ' edd-wl-item-purchase'; $wrapper_class .= $wrapper_class ? $default_wrapper_class : trim($default_wrapper_class); $default_css_class = apply_filters('edd_wl_item_purchase_default_css_classes', 'edd-add-to-cart-from-wish-list', $download_id); ?> <form id="<?php echo $form_id; ?> " class="edd_download_purchase_form" method="post"> <div class="edd_purchase_submit_wrapper"> <?php printf('<a href="%10$s" class="%11$s %1$s %8$s" data-action="edd_add_to_cart_from_wish_list" data-download-id="%3$s" %4$s %5$s %6$s %7$s><span class="label">%2$s</span>%9$s</a>', implode(' ', array($style, $color, trim($class))), esc_attr($text), esc_attr($download_id), esc_attr($data_variable), esc_attr($type), $button_display, esc_attr($data_price_option), $button_size, $loading, $link, $default_css_class); // checkout link that shows when item is added to the cart printf('<a href="%1$s" class="%2$s %3$s %5$s" %4$s>' . $checkout_text . '</a>', esc_url(edd_get_checkout_uri()), esc_attr('edd-go-to-checkout-from-wish-list'), implode(' ', array($style, $color, trim($class))), $checkout_display, $button_size); ?> </div> </form> <?php $html = '<' . $wrapper . ' class="' . $wrapper_class . '"' . '>' . ob_get_clean() . '</' . $wrapper . '>'; return apply_filters('edd_wl_item_purchase', $html, $item); }
/** * Get Purchase Link * * Builds a Purchase link for a specified download based on arguments passed. * This function is used all over EDD to generate the Purchase or Add to Cart * buttons. If no arguments are passed, the function uses the defaults that have * been set by the plugin. The Purchase link is built for simple and variable * pricing and filters are available throughout the function to override * certain elements of the function. * * $download_id = null, $link_text = null, $style = null, $color = null, $class = null * * @since 1.0 * @param array $args Arguments for display * @return string $purchase_form */ function pp_get_purchase_link($args = array()) { global $post, $edd_displayed_form_ids; $purchase_page = edd_get_option('purchase_page', false); if (!$purchase_page || $purchase_page == 0) { edd_set_error('set_checkout', sprintf(__('No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'edd'), admin_url('edit.php?post_type=download&page=edd-settings'))); edd_print_errors(); return false; } $post_id = is_object($post) ? $post->ID : 0; $defaults = apply_filters('edd_purchase_link_defaults', array('download_id' => $post_id, 'price' => (bool) true, 'price_id' => isset($args['price_id']) ? $args['price_id'] : false, 'direct' => edd_get_download_button_behavior($post_id) == 'direct' ? true : false, 'text' => edd_get_option('add_to_cart_text', __('Purchase', 'edd')), 'style' => edd_get_option('button_style', 'button'), 'color' => edd_get_option('checkout_color', 'blue'), 'class' => 'edd-submit')); $args = wp_parse_args($args, $defaults); // Override the stright_to_gateway if the shop doesn't support it if (!edd_shop_supports_buy_now()) { $args['direct'] = false; } $download = new EDD_Download($args['download_id']); if (empty($download->ID)) { return false; } if ('publish' !== $download->post_status && !current_user_can('edit_product', $download->ID)) { return false; // Product not published or user doesn't have permission to view drafts } // Override color if color == inherit $args['color'] = $args['color'] == 'inherit' ? '' : $args['color']; $options = array(); $variable_pricing = $download->has_variable_prices(); $data_variable = $variable_pricing ? ' data-variable-price="yes"' : 'data-variable-price="no"'; $type = $download->is_single_price_mode() ? 'data-price-mode=multi' : 'data-price-mode=single'; $show_price = $args['price'] && $args['price'] !== 'no'; $data_price_value = 0; if ($variable_pricing && false !== $args['price_id']) { $price_id = $args['price_id']; $prices = $download->prices; $options['price_id'] = $args['price_id']; $found_price = isset($prices[$price_id]) ? $prices[$price_id]['amount'] : false; $data_price_value = $found_price; if ($show_price) { $price = $found_price; } } elseif (!$variable_pricing) { $data_price_value = $download->price; if ($show_price) { $price = $download->price; } } $data_price = 'data-price="' . $data_price_value . '"'; $button_text = !empty($args['text']) ? ' – ' . $args['text'] : ''; if (isset($price) && false !== $price) { if (0 == $price) { $args['text'] = __('Free', 'edd') . $button_text; } else { $args['text'] = edd_currency_filter(edd_format_amount($price)) . $button_text; } } if (edd_item_in_cart($download->ID, $options) && (!$variable_pricing || !$download->is_single_price_mode())) { $button_display = 'style="display:none;"'; $checkout_display = ''; } else { $button_display = ''; $checkout_display = 'style="display:none;"'; } // Collect any form IDs we've displayed already so we can avoid duplicate IDs if (isset($edd_displayed_form_ids[$download->ID])) { $edd_displayed_form_ids[$download->ID]++; } else { $edd_displayed_form_ids[$download->ID] = 1; } $form_id = !empty($args['form_id']) ? $args['form_id'] : 'edd_purchase_' . $download->ID; // If we've already generated a form ID for this download ID, apped -# if ($edd_displayed_form_ids[$download->ID] > 1) { $form_id .= '-' . $edd_displayed_form_ids[$download->ID]; } $args = apply_filters('edd_purchase_link_args', $args); ob_start(); ?> <form id="<?php echo $form_id; ?> " class="edd_download_purchase_form edd_purchase_<?php echo absint($download->ID); ?> " method="post"> <?php do_action('edd_purchase_link_top', $download->ID, $args); ?> <div class="edd_purchase_submit_wrapper"> <?php $class = implode(' ', array($args['style'], $args['color'], trim($args['class']))); // custom to pippinsplugins.com if (!edd_is_ajax_disabled()) { echo '<a href="#" class="edd-add-to-cart ' . esc_attr($class) . '" data-action="edd_add_to_cart" data-download-id="' . esc_attr($download->ID) . '" ' . $data_variable . ' ' . $type . ' ' . $data_price . ' ' . $button_display . '><span class="edd-add-to-cart-label">' . $args['text'] . '</span> <span class="edd-loading"><img class="edd-icon-spin" src="' . get_stylesheet_directory_uri() . "/svgs/spinner.svg" . ' " /></span></a>'; } printf('<input type="submit" class="edd-add-to-cart edd-no-js %1$s" name="edd_purchase_download" value="%2$s" data-action="edd_add_to_cart" data-download-id="%3$s" %4$s %5$s %6$s/>', implode(' ', array($args['style'], $args['color'], trim($args['class']))), esc_attr($args['text']), esc_attr($download->ID), esc_attr($data_variable), esc_attr($type), $button_display); printf('<a href="%1$s" class="%2$s %3$s" %4$s>' . __('Checkout', 'edd') . '%5$s</a>', esc_url(edd_get_checkout_uri()), esc_attr('edd_go_to_checkout'), implode(' ', array($args['style'], $args['color'], trim($args['class']))), $checkout_display, pp_add_to_cart_success()); // end custom to pippinsplugins.com ?> <?php if (!$download->is_free($args['price_id'])) { ?> <?php if (edd_display_tax_rate() && edd_prices_include_tax()) { echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Includes %1$s% tax', 'edd'), edd_get_tax_rate() * 100) . '</span>'; } elseif (edd_display_tax_rate() && !edd_prices_include_tax()) { echo '<span class="edd_purchase_tax_rate">' . sprintf(__('Excluding %1$s% tax', 'edd'), edd_get_tax_rate() * 100) . '</span>'; } ?> <?php } ?> </div> <input type="hidden" name="download_id" value="<?php echo esc_attr($download->ID); ?> "> <?php if ($variable_pricing && isset($price_id) && isset($prices[$price_id])) { ?> <input type="hidden" name="edd_options[price_id][]" id="edd_price_option_<?php echo $download->ID; ?> _1" class="edd_price_option_<?php echo $download->ID; ?> " value="<?php echo $price_id; ?> "> <?php } ?> <?php if (!empty($args['direct']) && !$download->is_free($args['price_id'])) { ?> <input type="hidden" name="edd_action" class="edd_action_input" value="straight_to_gateway"> <?php } else { ?> <input type="hidden" name="edd_action" class="edd_action_input" value="add_to_cart"> <?php } ?> <?php do_action('edd_purchase_link_end', $download->ID, $args); ?> </form><!--end #<?php echo esc_attr($form_id); ?> --> <?php $purchase_form = ob_get_clean(); return apply_filters('edd_purchase_download_form', $purchase_form, $args); }
/** * Display Cross-sell/Upsell products * * @since 1.0 */ function edd_csau_html($columns = '3') { global $post, $edd_options; // upsell products for the single download page if (is_singular('download')) { $products = edd_csau_get_products(get_the_ID(), 'upsell'); } elseif (edd_is_checkout()) { // get contents on the cart $cart_items = edd_get_cart_contents(); // return if there's nothing in the cart if (!$cart_items) { return; } $cart = array(); // create new products array with the cart items cross sell products if ($cart_items) { foreach ($cart_items as $cart_item) { $download_id = $cart_item['id']; // create $cart array with IDs $cart[] = (int) $cart_item['id']; // create $product_list array with cross sell products $product_list[] = get_post_meta($download_id, '_edd_csau_cross_sell_products', false); } } $products = $product_list; // clean the array $products = array_filter($products); // return if no cross sell products after clean if (!$products) { return; } // merge into single level array $products = call_user_func_array('array_merge', $products); // remove duplicate IDs $products = array_unique($products); } else { return; } if ($products) { ?> <?php if (edd_is_checkout()) { $posts_per_page = isset($edd_options['edd_csau_cross_sell_number']) && !empty($edd_options['edd_csau_cross_sell_number']) ? $edd_options['edd_csau_cross_sell_number'] : '3'; } elseif (is_singular('download')) { $posts_per_page = isset($edd_options['edd_csau_upsell_number']) && !empty($edd_options['edd_csau_upsell_number']) ? $edd_options['edd_csau_upsell_number'] : '3'; } $query = array('post_type' => 'download', 'posts_per_page' => $posts_per_page, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => $products); $query = apply_filters('edd_csau_query', $query); $downloads = new WP_Query($query); if ($downloads->have_posts()) { // upsell heading if (is_singular('download')) { $upsell_heading = get_post_meta(get_the_ID(), '_edd_csau_upsell_heading', true); // show singular heading if ($upsell_heading) { $heading = esc_attr($upsell_heading); } elseif (isset($edd_options['edd_csau_upsell_heading'])) { $heading = esc_attr($edd_options['edd_csau_upsell_heading']); } else { $heading = __('You may also like', 'edd-csau'); } } elseif (edd_is_checkout()) { $ids = edd_csau_get_cart_trigger_ids(); if (count($ids) == 1) { $heading = esc_attr(get_post_meta($ids[0], '_edd_csau_cross_sell_heading', true)); } elseif (isset($edd_options['edd_csau_cross_sell_heading'])) { $heading = esc_attr($edd_options['edd_csau_cross_sell_heading']); } else { $heading = __('You may also like', 'edd-csau'); } } // end is_checkout $i = 1; global $wp_query; //$download_count = $downloads->found_posts > 3 ? 3 : $downloads->found_posts; $classes = array(); $classes = apply_filters('edd_csau_classes', $classes); // default classes $classes[] = 'edd-csau-products'; // columns if ($columns) { $classes[] = 'col-' . $columns; } // filter array and remove empty values $classes = array_filter($classes); $classes = !empty($classes) ? implode(' ', $classes) : ''; $class_list = !empty($classes) ? 'class="' . $classes . '"' : ''; ob_start(); ?> <div <?php echo $class_list; ?> > <h2><?php echo esc_attr($heading); ?> </h2> <?php while ($downloads->have_posts()) { $downloads->the_post(); ?> <div itemscope itemtype="http://schema.org/Product" class="<?php echo apply_filters('edd_download_class', 'edd_download', '', ''); ?> " id="edd_download_<?php echo get_the_ID(); ?> "> <div class="edd_download_inner"> <?php do_action('edd_csau_download_before'); $show_excerpt = apply_filters('edd_csau_show_excerpt', true); $show_price = apply_filters('edd_csau_show_price', true); $show_button = apply_filters('edd_csau_upsell_show_button', true); edd_get_template_part('shortcode', 'content-image'); edd_get_template_part('shortcode', 'content-title'); if ($show_price) { edd_get_template_part('shortcode', 'content-price'); } if ($show_excerpt) { edd_get_template_part('shortcode', 'content-excerpt'); } // if the download is not in the cart, show the add to cart button if (edd_is_checkout()) { if (!edd_item_in_cart(get_the_ID())) { $text = apply_filters('edd_csau_cross_sell_add_to_cart_text', __('Add to cart', 'edd-csau')); $price = apply_filters('edd_csau_cross_sell_show_button_price', false); if ($show_button) { ?> <div class="edd_download_buy_button"> <?php echo edd_get_purchase_link(array('download_id' => get_the_ID(), 'text' => $text, 'price' => $price)); ?> </div> <?php } ?> <?php } else { echo apply_filters('edd_csau_added_to_cart_text', '<span class="edd-cart-added-alert"><i class="edd-icon-ok"></i> ' . __('Added to cart', 'edd-csau') . '</span>'); } } else { $text = apply_filters('edd_csau_upsell_add_to_cart_text', __('Add to cart', 'edd-csau')); $price = apply_filters('edd_csau_upsell_show_button_price', false); $show_button = apply_filters('edd_csau_upsell_show_button', true); if ($show_button) { ?> <div class="edd_download_buy_button"> <?php echo edd_get_purchase_link(array('download_id' => get_the_ID(), 'text' => $text, 'price' => $price)); ?> </div> <?php } ?> <?php } do_action('edd_csau_download_after'); ?> </div> </div> <?php if ($columns && $i % $columns == 0) { ?> <div style="clear:both;"></div><?php } ?> <?php $i++; } ?> <?php wp_reset_postdata(); ?> </div> <?php $html = ob_get_clean(); return apply_filters('edd_csau_html', $html, $downloads, $heading, $columns, $class_list); } ?> <?php } ?> <?php }
/** * Display variable price. * * Display the variable price with a strikethrough in the list. * NOTE! This function replaces an entire EDD function! * * @since 1.0.0 * * @param int $download_id ID of the download to get the labels for. * @param array $args Array of arguments related to the download price. */ public function edd_purchase_variable_pricing($download_id = 0, $args = array()) { global $edd_options; $variable_pricing = edd_has_variable_prices($download_id); $prices = apply_filters('edd_purchase_variable_prices', edd_get_variable_prices($download_id), $download_id); if (!$variable_pricing || false !== $args['price_id'] && isset($prices[$args['price_id']])) { return; } if (edd_item_in_cart($download_id) && !edd_single_price_option_mode($download_id)) { return; } $type = edd_single_price_option_mode($download_id) ? 'checkbox' : 'radio'; $mode = edd_single_price_option_mode($download_id) ? 'multi' : 'single'; do_action('edd_before_price_options', $download_id); ?> <div class="edd_price_options edd_<?php echo esc_attr($mode); ?> _mode"> <ul> <?php if ($prices) { $checked_key = isset($_GET['price_option']) ? absint($_GET['price_option']) : edd_get_default_variable_price($download_id); foreach ($prices as $key => $price) { ?> <li id="edd_price_option_<?php echo $download_id . '_' . sanitize_key($price['name']); ?> " itemprop="offers" itemscope itemtype="http://schema.org/Offer"> <label for="<?php echo esc_attr('edd_price_option_' . $download_id . '_' . $key); ?> "> <input type="<?php echo $type; ?> " <?php checked(apply_filters('edd_price_option_checked', $checked_key, $download_id, $key), $key); ?> name="edd_options[price_id][]" id="<?php echo esc_attr('edd_price_option_' . $download_id . '_' . $key); ?> " class="<?php echo esc_attr('edd_price_option_' . $download_id); ?> " value="<?php echo esc_attr($key); ?> "/> <span class='edd_price_option_wrap'> <span class="edd_price_option_name" itemprop="description"><?php echo esc_html($price['name']); ?> </span> <span class="edd_price_option_sep">–</span> <?php if (isset($price['sale_price']) && !empty($price['sale_price']) && isset($price['regular_amount'])) { ?> <span class="edd_price_option_price regular_price" itemprop="price"><del><?php echo edd_currency_filter(edd_format_amount($price['regular_amount'])); ?> </del></span> <?php } ?> <span class="edd_price_option_price" itemprop="price"><?php echo edd_currency_filter(edd_format_amount($price['amount'])); ?> </span> </span> </label><?php do_action('edd_after_price_option', $key, $price, $download_id); ?> </li><?php } } do_action('edd_after_price_options_list', $download_id, $prices, $type); ?> </ul> </div><!--end .edd_price_options--> <?php do_action('edd_after_price_options', $download_id); }
/** * 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']; } } }
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; }
/** * Ensure cart quantities are OK * * @since 1.0.0 * @return void */ function edd_pl_checkout_errors($valid_data, $posted) { global $edd_prices_sold_out; $cart = edd_get_cart_contents(); $scope = edd_get_option('edd_purchase_limit_scope') ? edd_get_option('edd_purchase_limit_scope') : 'site-wide'; $errors = array(); foreach ($cart as $item) { if (edd_has_variable_prices($item['id'])) { if (edd_pl_is_item_sold_out($item['id'], $item['options']['price_id'], false, false)) { $errors[] = array('id' => $item['id'], 'price' => $item['options']['price_id'], 'type' => 'soldout', 'avail' => null); } } else { $max_purchases = edd_pl_get_file_purchase_limit($item['id']); if ($scope == 'site-wide') { $purchases = edd_get_download_sales_stats($item['id']); if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) { $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null); } } else { if (is_user_logged_in()) { $purchases = edd_pl_get_user_purchase_count(get_current_user_id(), $item['id']); if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) { $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null); } } } } if (edd_item_in_cart($item['id'])) { if (edd_has_variable_prices($item['id'])) { $max_purchases = edd_pl_get_file_purchase_limit($item['id'], null, $item['options']['price_id']); $purchases = edd_pl_get_file_purchases($item['id'], $item['options']['price_id']); } if ($max_purchases > 0) { $cart_qty = edd_get_cart_item_quantity($item['id']); $total = $purchases + $cart_qty; if ($total > $max_purchases) { $errors[] = array('id' => $item['id'], 'price' => edd_has_variable_prices($item['id']) ? $item['options']['price_id'] : null, 'type' => 'toomany', 'avail' => $max_purchases - $purchases); } } } } if (count($errors) > 0) { foreach ($errors as $error) { $product = get_post($error['id']); if ($error['type'] == 'soldout') { edd_set_error('purchase_limit_reached', sprintf(__('The %s "%s" is sold out!', 'edd-purchase-limit'), strtolower(edd_get_label_singular()), $product->post_title)); } elseif ($error['type'] == 'toomany') { edd_set_error('purchase_limit_exceeded', sprintf(_n('There is only %s available for the %s "%s"!', 'There are only %s available for the %s "%s"!', $error['avail'], 'edd-purchase-limit'), $error['avail'], strtolower(edd_get_label_singular()), $product->post_title)); } } } }
function digitalstore_add_to_cart_callback($post) { global $edd_options; $out = '<div class="button-group add-to-cart clearfix">'; if (!edd_item_in_cart($post->ID)) { $show_price = edd_has_variable_prices($post->ID) ? '' : ' price="0"'; $out .= do_shortcode('[purchase_link id="' . $post->ID . '"' . $show_price . ' text="' . esc_attr__('Add To Cart', 'edd-digitalstore') . '" style="text"]'); } else { $out .= '<a href="' . get_permalink($edd_options['purchase_page']) . '" class="edd_go_to_checkout edd_button">' . __('Checkout', 'edd-digitalstore') . '</a>'; } $out .= '<button class="dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu">'; $title = urlencode(get_the_title($post->ID)); $permalink = urlencode(get_permalink($post->ID)); if (!edd_item_in_cart($post->ID)) { $actions = array('buy' => array('href' => '#addtocart', 'title' => sprintf(__('Buy %s', 'edd-digitalstore'), $title), 'text' => __('Buy this File', 'edd-digitalstore'))); } else { $actions = array('checkout' => array('href' => get_permalink($edd_options['purchase_page']), 'title' => __('Checkout', 'edd-digitalstore'), 'text' => __('Go to Checkout', 'edd-digitalstore'))); } $filterable_actions = array('twitter' => array('href' => sprintf('http://twitter.com/home?status=%s', urlencode(sprintf(__('Check this out: %s', 'edd-digitalstore'), $permalink))), 'title' => sprintf(__('Share %s on Twitter', 'edd-digitalstore'), $title), 'text' => __('Share on Twitter', 'edd-digitalstore')), 'googleplus' => array('href' => sprintf('https://plus.google.com/share?url=%s', urlencode($permalink)), 'title' => sprintf(__('Add %s to Google Plus', 'edd-digitalstore'), $title), 'text' => __('Add to Google+', 'edd-digitalstore')), 'facebook' => array('href' => sprintf('http://www.facebook.com/sharer.php?u=%s&t=%s', urlencode($permalink), urlencode(__('Check this out', 'edd-digitalstore'))), 'title' => sprintf(__('Share %s on Facebook', 'edd-digitalstore'), $title), 'text' => __('Share on Facebook', 'edd-digitalstore'))); $actions = array_merge($actions, apply_filters('digitalstore_add_to_cart_actions', $filterable_actions, $post, $title, $permalink)); foreach ($actions as $action) { $out .= '<li><a href="' . $action['href'] . '" title="' . esc_attr($action['title']) . '">' . $action['text'] . '</a></li>'; } $out .= '</ul>'; $out .= '</div>'; echo $out; }
/** * Retrieve all active fees * * @access public * @since 1.5 * @param string $type Fee type, "fee" or "item" * @param int $download_id The download ID whose fees to retrieve * @param int $price_id The variable price ID whose fees to retrieve * @uses EDD_Session::get() * @return array|bool List of fees when available, false when there are no fees */ public function get_fees($type = 'fee', $download_id = 0, $price_id = NULL) { $fees = EDD()->session->get('edd_cart_fees'); if (!edd_get_cart_contents()) { // We can only get item type fees when the cart is empty $type = 'item'; } if (!empty($fees) && !empty($type) && 'all' !== $type) { foreach ($fees as $key => $fee) { if (!empty($fee['type']) && $type != $fee['type']) { unset($fees[$key]); } } } if (!empty($fees) && !empty($download_id)) { // Remove fees that don't belong to the specified Download foreach ($fees as $key => $fee) { if ((int) $download_id !== (int) $fee['download_id']) { unset($fees[$key]); } } } // Now that we've removed any fees that are for other Downloads, lets also remove any fees that don't match this price id if (!empty($fees) && !empty($download_id) && !is_null($price_id)) { // Remove fees that don't belong to the specified Download AND Price ID foreach ($fees as $key => $fee) { if (is_null($fee['price_id'])) { continue; } if ((int) $price_id !== (int) $fee['price_id']) { unset($fees[$key]); } } } if (!empty($fees)) { // Remove fees that belong to a specific download but are not in the cart foreach ($fees as $key => $fee) { if (empty($fee['download_id'])) { continue; } if (!edd_item_in_cart($fee['download_id'])) { unset($fees[$key]); } } } // Allow 3rd parties to process the fees before returning them return apply_filters('edd_fees_get_fees', !empty($fees) ? $fees : array(), $this); }
/** * Get Purchase Link * * Returns the purchase link. * * $download_id = null, $link_text = null, $style = null, $color = null, $class = null * * @access public * @since 1.0 * @return string */ function edd_get_purchase_link($args = array()) { global $edd_options, $post; if (!isset($edd_options['purchase_page']) || $edd_options['purchase_page'] == 0) { edd_set_error('set_checkout', sprintf(__('No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'edd'), admin_url('edit.php?post_type=download&page=edd-settings'))); edd_print_errors(); return false; } $defaults = array('download_id' => $post->ID, 'price' => (bool) true, 'text' => isset($edd_options['add_to_cart_text']) && $edd_options['add_to_cart_text'] != '' ? $edd_options['add_to_cart_text'] : __('Purchase', 'edd'), 'style' => isset($edd_options['button_style']) ? $edd_options['button_style'] : 'button', 'color' => isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'blue', 'class' => 'edd-submit'); $args = wp_parse_args($args, $defaults); $variable_pricing = edd_has_variable_prices($args['download_id']); $data_variable = $variable_pricing ? ' data-variable-price="yes"' : ''; if ($args['price'] && !$variable_pricing) { $price = edd_get_download_price($args['download_id']); if (edd_use_taxes() && edd_taxes_on_prices()) { $price += edd_calculate_tax($price); } $args['text'] = edd_currency_filter(edd_format_amount($price)) . ' – ' . $args['text']; } if (edd_item_in_cart($args['download_id'])) { $button_display = 'style="display:none;"'; $checkout_display = ''; } else { $button_display = ''; $checkout_display = 'style="display:none;"'; } ob_start(); ?> <form id="edd_purchase_<?php echo $args['download_id']; ?> " class="edd_download_purchase_form" method="post"> <?php do_action('edd_purchase_link_top', $args['download_id']); ?> <div class="edd_purchase_submit_wrapper"> <?php printf('<input type="submit" class="edd-add-to-cart %1$s" name="edd_purchase_download" value="%2$s" data-action="edd_add_to_cart" data-download-id="%3$s" %4$s %5$s/>', implode(' ', array($args['style'], $args['color'], trim($args['class']))), esc_attr($args['text']), esc_attr($args['download_id']), esc_attr($data_variable), $button_display); printf('<a href="%1$s" class="%2$s %3$s" %4$s>' . __('Checkout', 'edd') . '</a>', esc_url(edd_get_checkout_uri()), esc_attr('edd_go_to_checkout'), implode(' ', array($args['style'], $args['color'], trim($args['class']))), $checkout_display); ?> <?php if (edd_is_ajax_enabled()) { ?> <span class="edd-cart-ajax-alert"> <img src="<?php echo esc_url(EDD_PLUGIN_URL . 'assets/images/loading.gif'); ?> " class="edd-cart-ajax" style="display: none;" /> <span class="edd-cart-added-alert" style="display: none;">—<?php _e('Item successfully added to your cart.', 'edd'); ?> </span> </span> <?php } ?> </div><!--end .edd_purchase_submit_wrapper--> <input type="hidden" name="download_id" value="<?php echo esc_attr($args['download_id']); ?> "> <input type="hidden" name="edd_action" value="add_to_cart"> <?php do_action('edd_purchase_link_end', $args['download_id']); ?> </form><!--end #edd_purchase_<?php echo esc_attr($args['download_id']); ?> --> <?php $purchase_form = ob_get_clean(); return apply_filters('edd_purchase_download_form', $purchase_form, $args); }
/** * Checks to see if the required products are in the cart * * @since 1.5 * @param int $code_id Discount ID * @return bool $ret Are required products in the cart? */ function edd_discount_product_reqs_met($code_id = null) { $product_reqs = edd_get_discount_product_reqs($code_id); $condition = edd_get_discount_product_condition($code_id); $excluded_ps = edd_get_discount_excluded_products($code_id); $cart_items = edd_get_cart_contents(); $cart_ids = $cart_items ? wp_list_pluck($cart_items, 'id') : null; $ret = false; if (empty($product_reqs) && empty($excluded_ps)) { $ret = true; } // Normalize our data for product requiremetns, exlusions and cart data // First absint the items, then sort, and reset the array keys $product_reqs = array_map('absint', $product_reqs); asort($product_reqs); $product_reqs = array_values($product_reqs); $excluded_ps = array_map('absint', $excluded_ps); asort($excluded_ps); $excluded_ps = array_values($excluded_ps); $cart_ids = array_map('absint', $cart_ids); asort($cart_ids); $cart_ids = array_values($cart_ids); // Ensure we have requirements before proceeding if (!$ret && !empty($product_reqs)) { switch ($condition) { case 'all': // Default back to true $ret = true; foreach ($product_reqs as $download_id) { if (!edd_item_in_cart($download_id)) { edd_set_error('edd-discount-error', __('The product requirements for this discount are not met.', 'edd')); $ret = false; break; } } break; default: // Any foreach ($product_reqs as $download_id) { if (edd_item_in_cart($download_id)) { $ret = true; break; } } if (!$ret) { edd_set_error('edd-discount-error', __('The product requirements for this discount are not met.', 'edd')); } break; } } else { $ret = true; } if (!empty($excluded_ps)) { // Check that there are products other than excluded ones in the cart if ($cart_ids == $excluded_ps) { edd_set_error('edd-discount-error', __('This discount is not valid for the cart contents.', 'edd')); $ret = false; } } return (bool) apply_filters('edd_is_discount_products_req_met', $ret, $code_id, $condition); }
/** * The template for displaying Archive pages. * * Used to display archive-type pages if nothing more specific matches a query. * For example, puts together date-based pages if no date.php file exists. * * Learn more: http://codex.wordpress.org/Template_Hierarchy */ global $wp_query; $data = Maera()->template->context(); $data['title'] = __('Downloads', 'maera_edd'); $data['posts'] = Timber::query_posts(false, 'TimberPost'); $data['query'] = $wp_query->query_vars; // The in-cart class $data['in_cart'] = function_exists('edd_item_in_cart') && edd_item_in_cart($post->ID) && !edd_has_variable_prices($post->ID) ? 'in-cart' : ''; // The variable-priced class $data['variable_priced'] = function_exists('edd_has_variable_prices') && edd_has_variable_prices($post->ID) ? 'variable-priced' : ''; // Get a list with categories of each download (Isotope filtering) $terms = get_the_terms($post->ID, 'download_category'); if ($terms && !is_wp_error($terms)) { foreach ($terms as $term) { $download_categories[] = $term->slug; } $data['categories'] = join(' ', $download_categories); } else { $data['categories'] = ''; } // Get a list with tags of each download (Isotope filtering) $terms = get_the_terms($post->ID, 'download_tag'); if ($terms && !is_wp_error($terms)) {
/** * Display the quantity field for a variable price when multi-purchase mode is enabled * * @since 2.2 * @param int $download_id Download ID * @param array $args Argument array * @return void */ function edd_download_purchase_form_quantity_field($download_id = 0, $args = array()) { $options = array(); if (false !== $args['price_id']) { $options['price_id'] = $args['price_id']; } if (!edd_item_quantities_enabled()) { return; } if (edd_item_in_cart($download_id) && !edd_has_variable_prices($download_id)) { return; } if (edd_single_price_option_mode($download_id) && edd_has_variable_prices($download_id) && !edd_item_in_cart($download_id, $options)) { return; } if (edd_single_price_option_mode($download_id) && edd_has_variable_prices($download_id) && edd_item_in_cart($download_id, $options)) { return; } if (!edd_single_price_option_mode($download_id) && edd_has_variable_prices($download_id) && edd_item_in_cart($download_id, $options)) { return; } ob_start(); ?> <div class="edd_download_quantity_wrapper"> <input type="number" min="1" step="1" name="edd_download_quantity" class="edd-input edd-item-quantity" value="1" /> </div> <?php $quantity_input = ob_get_clean(); echo apply_filters('edd_purchase_form_quantity_input', $quantity_input, $download_id, $args); }
/** * Filter [download] shortcode HTML * @since 1.0 */ function modify_edd_download_shortcode($display, $atts, $buy_button, $columns, $column_width, $downloads, $excerpt, $full_content, $price, $thumbnails, $query) { $button_defaults = apply_filters('edd_purchase_link_defaults', array()); $button_defaults_class = $button_defaults['class']; // We can't divide the grid to 5 columns, so we're setting them to 4. $columns = 5 == $columns ? 4 : $columns; if (1 == $columns) { $column_class = '[maera_grid_col_12]'; } else { if (2 == $columns) { $column_class = '[maera_grid_col_6]'; } else { if (3 == $columns) { $column_class = '[maera_grid_col_4]'; } else { if (4 == $columns) { $column_class = '[maera_grid_col_3]'; } else { if (6 == $columns) { $column_class = '[maera_grid_col_2]'; } } } } } ob_start(); $count = 0; $rand = rand(0, 999); if (1 != $columns) { echo '<style>.downloads-list .edd-grid-column-' . $rand . '_1{clear:left;}.downloads-list [class*="column"] + [class*="column"]:last-child{float: left;}</style>'; } $list_class = 1 == $columns ? 'list' : 'grid'; ?> <div class="downloads-list <?php echo $list_class; ?> "> <div class="[maera_grid_row_class]"> <?php while ($downloads->have_posts()) { $downloads->the_post(); $count++; $count = $count > $columns ? 1 : $count; $count_class = 1 < $columns ? 'edd-grid-column-' . $rand . '_' . $count : null; $in_cart = edd_item_in_cart(get_the_ID()) && !edd_has_variable_prices(get_the_ID()) ? 'in-cart' : ''; $variable_priced = edd_has_variable_prices(get_the_ID()) ? 'variable-priced' : ''; $hover_type = get_theme_mod('hover_type', 'edd'); $effect = 'effect-' . $hover_type; $context = Maera()->template->context(); $context['post'] = new TimberPost(get_the_ID()); $context['columns'] = $columns; $context['display_excerpt'] = $excerpt != 'no' && $full_content != 'yes' ? true : false; $context['display_full'] = 'yes' == $full_content ? true : false; $context['display_buy_btn'] = $buy_button; $context['in_cart'] = $in_cart; $context['variable_priced'] = $variable_priced; $context['column_class'] = $column_class; $context['count_class'] = $count_class; $context['count'] = $count; $context['download_classes'] = array($in_cart, $variable_priced, $column_class, $count_class, $count, $effect); $context['btn_class'] = $button_defaults_class; if (1 == $columns) { $mode = 'list'; $context['download_classes'] = array($in_cart, $variable_priced, $column_class, $count_class, $count); } else { $mode = $hover_type; $mode = 'edd' == $hover_type ? 'grid' : $mode; } Maera()->template->main('shortcode-download-content-' . $mode . '.twig', $context); } wp_reset_postdata(); ?> </div> </div> <div id="downloads-shortcode" class="download-navigation clearfix"> <?php $big = 999999; $paginate_links_args = array('base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 'format' => '?paged=%#%', 'current' => max(1, $query['paged']), 'total' => $downloads->max_num_pages, 'prev_next' => false, 'show_all' => true); echo paginate_links($paginate_links_args); ?> </div> <script>jQuery( "ul.page-numbers" ).addClass( "pagination" );</script> <?php $display = ob_get_clean(); return $display; }
/** * Add To Cart * * Adds a download ID to the shopping cart. * * @since 1.0 * * @param int $download_id Download IDs to be added to the cart * @param array $options Array of options, such as variable price * * @return string Cart key of the new item */ function edd_add_to_cart($download_id, $options = array()) { $download = get_post($download_id); if ('download' != $download->post_type) { return; } // Not a download product if (!current_user_can('edit_post', $download->ID) && $download->post_status != 'publish') { return; // Do not allow draft/pending to be purchased if can't edit. Fixes #1056 } do_action('edd_pre_add_to_cart', $download_id, $options); $cart = apply_filters('edd_pre_add_to_cart_contents', edd_get_cart_contents()); if (edd_has_variable_prices($download_id) && !isset($options['price_id'])) { // Forces to the first price ID if none is specified and download has variable prices $options['price_id'] = '0'; } if (isset($options['quantity'])) { $quantity = absint(preg_replace('/[^0-9\\.]/', '', $options['quantity'])); unset($options['quantity']); } else { $quantity = 1; } // If the price IDs are a string and is a coma separted list, make it an array (allows custom add to cart URLs) if (isset($options['price_id']) && !is_array($options['price_id']) && false !== strpos($options['price_id'], ',')) { $options['price_id'] = explode(',', $options['price_id']); } if (isset($options['price_id']) && is_array($options['price_id'])) { // Process multiple price options at once foreach ($options['price_id'] as $price) { $items[] = array('id' => $download_id, 'options' => array('price_id' => preg_replace('/[^0-9\\.-]/', '', $price)), 'quantity' => $quantity); } } else { // Sanitize price IDs foreach ($options as $key => $option) { if ('price_id' == $key) { $options[$key] = preg_replace('/[^0-9\\.-]/', '', $option); } } // Add a single item $items[] = array('id' => $download_id, 'options' => $options, 'quantity' => $quantity); } foreach ($items as $item) { $to_add = apply_filters('edd_add_to_cart_item', $item); if (!is_array($to_add)) { return; } if (!isset($to_add['id']) || empty($to_add['id'])) { return; } if (edd_item_in_cart($to_add['id'], $to_add['options']) && edd_item_quantities_enabled()) { $key = edd_get_item_position_in_cart($to_add['id'], $to_add['options']); $cart[$key]['quantity'] += $quantity; } else { $cart[] = $to_add; } } EDD()->session->set('edd_cart', $cart); do_action('edd_post_add_to_cart', $download_id, $options); // Clear all the checkout errors, if any edd_clear_errors(); return count($cart) - 1; }
/** * 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)); }