/** * Maybe add incentive discounts * * @since 1.0.1 * @param float $discount The current discount amount * @param array $item The cart item array * @return float $discount The updated discount amount */ function edd_wallet_item_incentive_amount($discount, $item) { $incentive_amount = edd_get_option('edd_wallet_incentive_amount', 0); if ($incentive_amount <= 0) { return $discount; } if (!EDD()->session->get('wallet_has_incentives')) { return $discount; } if (edd_has_variable_prices($item['id'])) { $prices = edd_get_variable_prices($item['id']); $price_id = isset($item['options']['price_id']) ? $item['options']['price_id'] : 0; if ($price_id !== false && $price_id !== '' && isset($prices[$price_id])) { $price = edd_get_price_option_amount($item['id'], $price_id); } else { $price = edd_get_lowest_price_option($item['id']); } } else { $price = edd_get_download_price($item['id']); } $incentive_type = edd_get_option('edd_wallet_incentive_type', 'flatrate'); if ($incentive_type == 'percent') { $incentive_amount /= 100; $incentive_amount = $price * $incentive_amount; if (edd_item_quantities_enabled() && edd_get_option('edd_wallet_incentive_quantities', false)) { $incentive_amount *= $item['quantity']; } $incentive_amount = number_format($incentive_amount, 2, '.', ''); } else { if (edd_item_quantities_enabled() && edd_get_option('edd_wallet_incentive_quantities', false)) { $incentive_amount *= $item['quantity']; } } $discount += $incentive_amount; return $discount; }
/** * Variable price output * * Outputs variable pricing options for each download or a specified downloads in a list. * The output generated can be overridden by the filters provided or by removing * the action and adding your own custom action. * * @since 1.2.3 * @param int $download_id Download ID * @return void */ function edd_purchase_variable_pricing($download_id = 0, $args = array()) { $variable_pricing = edd_has_variable_prices($download_id); if (!$variable_pricing) { return; } $prices = apply_filters('edd_purchase_variable_prices', edd_get_variable_prices($download_id), $download_id); // If the price_id passed is found in the variable prices, do not display all variable prices. if (false !== $args['price_id'] && isset($prices[$args['price_id']])) { return; } $type = edd_single_price_option_mode($download_id) ? 'checkbox' : 'radio'; $mode = edd_single_price_option_mode($download_id) ? 'multi' : 'single'; $schema = edd_add_schema_microdata() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : ''; if (edd_item_in_cart($download_id) && !edd_single_price_option_mode($download_id)) { return; } 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) { echo '<li id="edd_price_option_' . $download_id . '_' . sanitize_key($price['name']) . '"' . $schema . '>'; echo '<label for="' . esc_attr('edd_price_option_' . $download_id . '_' . $key) . '">'; echo '<input type="' . $type . '" ' . checked(apply_filters('edd_price_option_checked', $checked_key, $download_id, $key), $key, false) . ' name="edd_options[price_id][]" id="' . esc_attr('edd_price_option_' . $download_id . '_' . $key) . '" class="' . esc_attr('edd_price_option_' . $download_id) . '" value="' . esc_attr($key) . '" data-price="' . edd_get_price_option_amount($download_id, $key) . '"/> '; echo '<span class="edd_price_option_name" itemprop="description">' . esc_html($price['name']) . '</span><span class="edd_price_option_sep"> – </span><span class="edd_price_option_price" itemprop="price">' . edd_currency_filter(edd_format_amount($price['amount'])) . '</span>'; echo '</label>'; do_action('edd_after_price_option', $key, $price, $download_id); echo '</li>'; } } 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); }
/** * Checks whether or not a download is free * * @since 2.1 * @author Daniel J Griffiths * @param int $download_id ID number of the download to check * @param int $price_id (Optional) ID number of a variably priced item to check * @return bool $is_free True if the product is free, false if the product is not free or the check fails */ function edd_is_free_download($download_id = 0, $price_id = false) { $is_free = false; $variable_pricing = edd_has_variable_prices($download_id); if ($variable_pricing && !is_null($price_id) && $price_id !== false) { $price = edd_get_price_option_amount($download_id, $price_id); } elseif (!$variable_pricing) { $price = get_post_meta($download_id, 'edd_price', true); } if (isset($price) && (double) $price == 0) { $is_free = true; } return (bool) apply_filters('edd_is_free_download', $is_free, $download_id, $price_id); }
/** * Determine if the download is free or if the given price ID is free * * @since 2.2 * @return bool */ public function is_free($price_id = false) { $is_free = false; $variable_pricing = edd_has_variable_prices($this->ID); if ($variable_pricing && !is_null($price_id) && $price_id !== false) { $price = edd_get_price_option_amount($this->ID, $price_id); } elseif ($variable_pricing && $price_id === false) { $lowest_price = (double) edd_get_lowest_price_option($this->ID); $highest_price = (double) edd_get_highest_price_option($this->ID); if ($lowest_price === 0.0 && $highest_price === 0.0) { $price = 0; } } elseif (!$variable_pricing) { $price = get_post_meta($this->ID, 'edd_price', true); } if (isset($price) && (double) $price == 0) { $is_free = true; } return (bool) apply_filters('edd_is_free_download', $is_free, $this->ID, $price_id); }
/** * Undos a purchase, including the decrease of sale and earning stats. Used for * when refunding or deleting a purchase * * @since 1.0.8.1 * @param int $download_id Download (Post) ID * @param int $payment_id Payment ID * @return void */ function edd_undo_purchase($download_id, $payment_id) { $cart_details = edd_get_payment_meta_cart_details($payment_id); $user_info = edd_get_payment_meta_user_info($payment_id); if (is_array($cart_details)) { foreach ($cart_details as $item) { // get the item's price $amount = isset($item['price']) ? $item['price'] : false; // Decrease earnings/sales and fire action once per quantity number for ($i = 0; $i < $item['quantity']; $i++) { // variable priced downloads if (false === $amount && edd_has_variable_prices($download_id)) { $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null; $amount = !isset($item['price']) && 0 !== $item['price'] ? edd_get_price_option_amount($download_id, $price_id) : $item['price']; } if (!$amount) { // This function is only used on payments with near 1.0 cart data structure $amount = edd_get_download_final_price($download_id, $user_info, $amount); } } // decrease earnings edd_decrease_earnings($download_id, $amount); // decrease purchase count edd_decrease_purchase_count($download_id, $item['quantity']); } } }
/** * Undos a purchase, including the decrease of sale and earning stats. Used for * when refunding or deleting a purchase * * @since 1.0.8.1 * @param int $download_id Download (Post) ID * @param int $payment_id Payment ID * @return void */ function edd_undo_purchase($download_id = false, $payment_id) { /** * In 2.5.7, a bug was found that $download_id was an incorrect usage. Passing it in * now does nothing, but we're holding it in place for legacy support of the argument order. */ if (!empty($download_id)) { $download_id = false; _edd_deprected_argument('download_id', 'edd_undo_purchase', '2.5.7'); } $payment = new EDD_Payment($payment_id); $cart_details = $payment->cart_details; $user_info = $payment->user_info; if (is_array($cart_details)) { foreach ($cart_details as $item) { // get the item's price $amount = isset($item['price']) ? $item['price'] : false; // Decrease earnings/sales and fire action once per quantity number for ($i = 0; $i < $item['quantity']; $i++) { // variable priced downloads if (false === $amount && edd_has_variable_prices($item['id'])) { $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null; $amount = !isset($item['price']) && 0 !== $item['price'] ? edd_get_price_option_amount($item['id'], $price_id) : $item['price']; } if (!$amount) { // This function is only used on payments with near 1.0 cart data structure $amount = edd_get_download_final_price($item['id'], $user_info, $amount); } } $maybe_decrease_earnings = apply_filters('edd_decrease_earnings_on_undo', true, $payment, $item['id']); if (true === $maybe_decrease_earnings) { // decrease earnings edd_decrease_earnings($item['id'], $amount); } $maybe_decrease_sales = apply_filters('edd_decrease_sales_on_undo', true, $payment, $item['id']); if (true === $maybe_decrease_sales) { // decrease purchase count edd_decrease_purchase_count($item['id'], $item['quantity']); } } } }
/** * Variable price output * * Outputs variable pricing options for each download or a specified downloads in a list. * The output generated can be overridden by the filters provided or by removing * the action and adding your own custom action. * * @since 1.2.3 * @param int $download_id Download ID * @return void */ function edd_purchase_variable_pricing($download_id = 0, $args = array()) { global $edd_displayed_form_ids; // If we've already generated a form ID for this download ID, append -# $form_id = ''; if ($edd_displayed_form_ids[$download_id] > 1) { $form_id .= '-' . $edd_displayed_form_ids[$download_id]; } $variable_pricing = edd_has_variable_prices($download_id); if (!$variable_pricing) { return; } $prices = apply_filters('edd_purchase_variable_prices', edd_get_variable_prices($download_id), $download_id); // If the price_id passed is found in the variable prices, do not display all variable prices. if (false !== $args['price_id'] && isset($prices[$args['price_id']])) { return; } $type = edd_single_price_option_mode($download_id) ? 'checkbox' : 'radio'; $mode = edd_single_price_option_mode($download_id) ? 'multi' : 'single'; $schema = edd_add_schema_microdata() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : ''; // Filter the class names for the edd_price_options div $css_classes_array = apply_filters('edd_price_options_classes', array('edd_price_options', 'edd_' . esc_attr($mode) . '_mode'), $download_id); // Sanitize those class names and form them into a string $css_classes_string = implode(array_map('sanitize_html_class', $css_classes_array), ' '); if (edd_item_in_cart($download_id) && !edd_single_price_option_mode($download_id)) { return; } do_action('edd_before_price_options', $download_id); ?> <div class="<?php echo esc_attr(rtrim($css_classes_string)); ?> "> <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) { echo '<li id="edd_price_option_' . $download_id . '_' . sanitize_key($price['name']) . $form_id . '"' . $schema . '>'; echo '<label for="' . esc_attr('edd_price_option_' . $download_id . '_' . $key . $form_id) . '">'; echo '<input type="' . $type . '" ' . checked(apply_filters('edd_price_option_checked', $checked_key, $download_id, $key), $key, false) . ' name="edd_options[price_id][]" id="' . esc_attr('edd_price_option_' . $download_id . '_' . $key . $form_id) . '" class="' . esc_attr('edd_price_option_' . $download_id) . '" value="' . esc_attr($key) . '" data-price="' . edd_get_price_option_amount($download_id, $key) . '"/> '; $item_prop = edd_add_schema_microdata() ? ' itemprop="description"' : ''; echo '<span class="edd_price_option_name"' . $item_prop . '>' . esc_html($price['name']) . '</span><span class="edd_price_option_sep"> – </span><span class="edd_price_option_price">' . edd_currency_filter(edd_format_amount($price['amount'])) . '</span>'; if (edd_add_schema_microdata()) { echo '<meta itemprop="price" content="' . esc_attr($price['amount']) . '" />'; echo '<meta itemprop="priceCurrency" content="' . esc_attr(edd_get_currency()) . '" />'; } echo '</label>'; do_action('edd_after_price_option', $key, $price, $download_id); echo '</li>'; } } 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); }
/** * Retrieve the cost to upgrade a license * * @since 3.3 * @return float */ function edd_sl_get_license_upgrade_cost($license_id = 0, $upgrade_id = 0) { $url = home_url(); $download_id = edd_software_licensing()->get_download_id($license_id); $upgrades = edd_sl_get_upgrade_paths($download_id); if (edd_has_variable_prices($download_id)) { $price_id = edd_software_licensing()->get_price_id($license_id); if (false !== $price_id && '' !== $price_id) { $old_price = edd_get_price_option_amount($download_id, $price_id); } else { $old_price = edd_get_lowest_price_option($download_id); } } else { $old_price = edd_get_download_price($download_id); } if (isset($upgrades[$upgrade_id]['price_id']) && false !== $upgrades[$upgrade_id]['price_id']) { $new_price = edd_get_price_option_amount($upgrades[$upgrade_id]['download_id'], $upgrades[$upgrade_id]['price_id']); } else { $new_price = edd_get_download_price($upgrades[$upgrade_id]['download_id']); } $cost = $new_price; if (!empty($upgrades[$upgrade_id]['pro_rated'])) { $cost -= $old_price; } if (isset($upgrades[$upgrade_id]['discount'])) { $cost -= $upgrades[$upgrade_id]['discount']; } if ($cost < 0) { $cost = 0; } return apply_filters('edd_sl_license_upgrade_cost', $cost, $license_id, $upgrade_id); }
/** * Displays a formatted price for a download * * @since 1.0 * @param int $download_id ID of the download price to show * @param bool $echo Whether to echo or return the results * @param int $price_id Optional price id for variable pricing * @return void */ function edd_price($download_id = 0, $echo = true, $price_id = false) { if (empty($download_id)) { $download_id = get_the_ID(); } if (edd_has_variable_prices($download_id)) { $prices = edd_get_variable_prices($download_id); if (false !== $price_id && isset($prices[$price_id])) { $price = edd_get_price_option_amount($download_id, $price_id); } elseif ($default = edd_get_default_variable_price($download_id)) { $price = edd_get_price_option_amount($download_id, $default); } else { $price = edd_get_lowest_price_option($download_id); } $price = edd_sanitize_amount($price); } else { $price = edd_get_download_price($download_id); } $price = apply_filters('edd_download_price', edd_sanitize_amount($price), $download_id, $price_id); $formatted_price = '<span class="edd_price" id="edd_price_' . $download_id . '">' . $price . '</span>'; $formatted_price = apply_filters('edd_download_price_after_html', $formatted_price, $download_id, $price, $price_id); if ($echo) { echo $formatted_price; } else { return $formatted_price; } }
/** * Undos a purchase, including the decrease of sale and earning stats. Used for * when refunding or deleting a purchase * * @since 1.1 * @todo make sure it works with quantities * @param int $download_id Download (Post) ID * @param int $payment_id Payment ID * @return void */ function edd_csau_undo_purchase($download_id, $payment_id, $type) { if (edd_is_test_mode()) { return; } // Don't undo if we are in test mode! $payment = get_post($payment_id); edd_csau_decrease_purchase_count($download_id, $type); $user_info = edd_get_payment_meta_user_info($payment_id); $cart_details = edd_get_payment_meta_cart_details($payment_id); $amount = null; if (is_array($cart_details) && edd_has_variable_prices($download_id)) { $cart_item_id = array_search($download_id, $cart_details); $price_id = isset($cart_details[$cart_item_id]['price']) ? $cart_details[$cart_item_id]['price'] : null; $amount = edd_get_price_option_amount($download_id, $price_id); } $amount = edd_get_download_final_price($download_id, $user_info, $amount); edd_csau_decrease_earnings($download_id, $amount, $type); }
$renewal_discount = edd_get_option('edd_sl_renewal_discount', false); if (empty($renewal_discount)) { return; } if ($renewal_discount < 1) { $renewal_discount *= 100; } $discount_amount = edd_get_cart_subtotal() * $renewal_discount / 100; $discount_amount = edd_currency_filter(edd_format_amount($discount_amount)); ?> <tr class="edd_cart_footer_row edd_sl_renewal_row"> <td colspan="3"><?php printf(__('License renewal discount: %s - %s', 'edd_plans'), $renewal_discount . '%', $discount_amount); ?> </td> </tr> <?php } //add_action( 'edd_cart_items_after', 'edd_plans_sl_cart_items_renewal_row' );
/** * @since 3.4 * @param $item array the cart item array * @return float */ function edd_sl_get_renewal_discount_amount($item = array(), $license_key = '') { $discount = 0.0; $license_id = edd_software_licensing()->get_license_by_key($license_key); if (edd_has_variable_prices($item['id'])) { $price_id = isset($options['price_id']) ? $options['price_id'] : (int) edd_software_licensing()->get_price_id($license_id); $prices = edd_get_variable_prices($item['id']); if (false !== $price_id && '' !== $price_id && isset($prices[$price_id])) { $price = edd_get_price_option_amount($item['id'], $price_id); } else { $price = edd_get_lowest_price_option($item['id']); } } else { $price = edd_get_download_price($item['id']); } $renewal_discount_percentage = edd_sl_get_renewal_discount_percentage($license_id); if ($renewal_discount_percentage) { $renewal_discount = $price * ($renewal_discount_percentage / 100); // todo: fix this. number_format returns a string. we should not perform math on strings. $renewal_discount = number_format($renewal_discount, 2, '.', ''); $discount += $renewal_discount; } return apply_filters('edd_sl_get_renewal_discount_amount', $discount, $license_key, $item); }