/** * Checks to see if multiple price options can be purchased at once * * @since 1.4.2 * @param int $download_id Download ID * @return bool */ function edd_single_price_option_mode($download_id = 0) { if (empty($download_id)) { $download = get_post(); $download_id = isset($download->ID) ? $download->ID : 0; } if (empty($download_id)) { return false; } $download = new EDD_Download($download_id); return $download->is_single_price_mode(); }
/** * 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 $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.', 'easy-digital-downloads'), admin_url('edit.php?post_type=download&page=edd-settings'))); edd_print_errors(); return false; } $post_id = is_object($post) ? $post->ID : 0; $button_behavior = edd_get_download_button_behavior($post_id); $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' => $button_behavior == 'direct' ? true : false, 'text' => $button_behavior == 'direct' ? edd_get_option('buy_now_text', __('Buy Now', 'easy-digital-downloads')) : edd_get_option('add_to_cart_text', __('Purchase', 'easy-digital-downloads')), '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; $price = false; 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; } } $args['display_price'] = $data_price_value; $data_price = 'data-price="' . $data_price_value . '"'; $button_text = !empty($args['text']) ? ' – ' . $args['text'] : ''; if (false !== $price) { if (0 == $price) { $args['text'] = __('Free', 'easy-digital-downloads') . $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']))); 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"><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($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', 'easy-digital-downloads') . '</a>'; ?> <?php if (!edd_is_ajax_disabled()) { ?> <span class="edd-cart-ajax-alert"> <span class="edd-cart-added-alert" style="display: none;"> <?php echo '<i class="edd-icon-ok"></i> ' . __('Added to cart', 'easy-digital-downloads'); ?> </span> </span> <?php } ?> <?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', 'easy-digital-downloads'), 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', 'easy-digital-downloads'), edd_get_tax_rate() * 100) . '</span>'; } ?> <?php } ?> </div><!--end .edd_purchase_submit_wrapper--> <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 if (apply_filters('edd_download_redirect_to_checkout', edd_straight_to_checkout(), $download->ID, $args)) { ?> <input type="hidden" name="edd_redirect_to_checkout" id="edd_redirect_to_checkout" value="1"> <?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); }