/** * Get Cart Discountable Subtotal. * * @return float Total discountable amount before taxes */ function edd_get_cart_discountable_subtotal($code_id) { $cart_items = edd_get_cart_content_details(); $items = array(); $excluded_products = edd_get_discount_excluded_products($code_id); if ($cart_items) { foreach ($cart_items as $item) { if (!in_array($item['id'], $excluded_products)) { $items[] = $item; } } } $subtotal = edd_get_cart_items_subtotal($items); return apply_filters('edd_get_cart_discountable_subtotal', $subtotal); }
/** * Returns the discounts for a certain download, returns an empty array if no discounts apply to this product. * * @param int $download_id The ID for the EDD download. * * @return array A list of discounts for the download. */ function download_get_discounts($download_id) { $discounts = edd_get_cart_discounts(); if (empty($discounts)) { $discounts = array(); } $discounts = array_map(function ($discount_code) { $discount_id = edd_get_discount_id_by_code($discount_code); return array('code' => $discount_code, 'id' => $discount_id, 'name' => get_post_meta($discount_id, '_edd_discount_name', true)); }, $discounts); return array_filter($discounts, function ($discount) use($download_id) { $requirements = edd_get_discount_product_reqs($discount['id']); $excluded = edd_get_discount_excluded_products($discount['id']); return in_array($download_id, $requirements) && !in_array($download_id, $excluded); }); }
* @subpackage Admin/Discounts * @copyright Copyright (c) 2014, Pippin Williamson * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 1.0 */ // Exit if accessed directly if (!defined('ABSPATH')) { exit; } if (!isset($_GET['discount']) || !is_numeric($_GET['discount'])) { wp_die(__('Something went wrong.', 'edd'), __('Error', 'edd')); } $discount_id = absint($_GET['discount']); $discount = edd_get_discount($discount_id); $product_reqs = edd_get_discount_product_reqs($discount_id); $excluded_products = edd_get_discount_excluded_products($discount_id); $condition = edd_get_discount_product_condition($discount_id); $single_use = edd_discount_is_single_use($discount_id); $flat_display = edd_get_discount_type($discount_id) == 'flat' ? '' : ' style="display:none;"'; $percent_display = edd_get_discount_type($discount_id) == 'percent' ? '' : ' style="display:none;"'; $condition_display = empty($product_reqs) ? ' style="display:none;"' : ''; ?> <h2><?php _e('Edit Discount', 'edd'); ?> - <a href="<?php echo admin_url('edit.php?post_type=download&page=edd-discounts'); ?> " class="button-secondary"><?php _e('Go Back', 'edd'); ?>
/** * Get the discounted amount on a price * * @since 1.9 * @param array $item Cart item array * @return float The discounted amount */ function edd_get_cart_item_discount_amount($item = array()) { global $edd_is_last_cart_item, $edd_flat_discount_total; // If we're not meeting the requirements of the $item array, return or set them if (empty($item) || empty($item['id'])) { return 0; } // Quantity is a requirement of the cart options array to determine the discounted price if (empty($item['quantity'])) { return 0; } if (!isset($item['options'])) { $item['options'] = array(); } $amount = 0; $price = edd_get_cart_item_price($item['id'], $item['options']); $discounted_price = $price; // Retrieve all discounts applied to the cart $discounts = edd_get_cart_discounts(); if ($discounts) { foreach ($discounts as $discount) { $code_id = edd_get_discount_id_by_code($discount); // Check discount exists if (!$code_id) { continue; } $reqs = edd_get_discount_product_reqs($code_id); $excluded_products = edd_get_discount_excluded_products($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 foreach ($reqs as $download_id) { if ($download_id == $item['id'] && !in_array($item['id'], $excluded_products)) { $discounted_price -= $price - edd_get_discounted_amount($discount, $price); } } } else { // This is a global cart discount if (!in_array($item['id'], $excluded_products)) { if ('flat' === edd_get_discount_type($code_id)) { /* * * In order to correctly record individual item amounts, global flat rate discounts * are distributed across all cart items. The discount amount is divided by the number * of items in the cart and then a portion is evenly applied to each cart item */ $items_subtotal = 0.0; $cart_items = edd_get_cart_contents(); foreach ($cart_items as $cart_item) { if (!in_array($cart_item['id'], $excluded_products)) { $item_price = edd_get_cart_item_price($cart_item['id'], $cart_item['options']); $items_subtotal += $item_price * $cart_item['quantity']; } } $subtotal_percent = $price * $item['quantity'] / $items_subtotal; $code_amount = edd_get_discount_amount($code_id); $discounted_amount = $code_amount * $subtotal_percent; $discounted_price -= $discounted_amount; $edd_flat_discount_total += round($discounted_amount, edd_currency_decimal_filter()); if ($edd_is_last_cart_item && $edd_flat_discount_total < $code_amount) { $adjustment = $code_amount - $edd_flat_discount_total; $discounted_price -= $adjustment; } } else { $discounted_price -= $price - edd_get_discounted_amount($discount, $price); } } } } $amount = $price - apply_filters('edd_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price); if ('flat' !== edd_get_discount_type($code_id)) { $amount = $amount * $item['quantity']; } } return $amount; }
/** * Get the discounted amount on a price * * @since 1.9 * @param array $item Cart item array * @return float The discounted amount */ function edd_get_cart_item_discount_amount($item = array()) { $amount = 0; $price = edd_get_cart_item_price($item['id'], $item['options'], edd_prices_include_tax()); $discounted_price = $price; // Retrieve all discounts applied to the cart $discounts = edd_get_cart_discounts(); if ($discounts) { foreach ($discounts as $discount) { $code_id = edd_get_discount_id_by_code($discount); $reqs = edd_get_discount_product_reqs($code_id); $excluded_products = edd_get_discount_excluded_products($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 foreach ($reqs as $download_id) { if ($download_id == $item['id'] && !in_array($item['id'], $excluded_products)) { $discounted_price -= $price - edd_get_discounted_amount($discount, $price); } } } else { // This is a global cart discount if (!in_array($item['id'], $excluded_products)) { if ('flat' === edd_get_discount_type($code_id)) { /* * * In order to correctly record individual item amounts, global flat rate discounts * are distributed across all cart items. The discount amount is divided by the number * of items in the cart and then a portion is evenly applied to each cart item */ $discounted_amount = edd_get_discount_amount($code_id); $discounted_amount = $discounted_amount / edd_get_cart_quantity(); $discounted_price -= $discounted_amount; } else { $discounted_price -= $price - edd_get_discounted_amount($discount, $price); } } } } $amount = $price - apply_filters('edd_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price); if ('flat' !== edd_get_discount_type($code_id)) { $amount = $amount * $item['quantity']; } } return $amount; }