/**
 * Get Download Final Price
 *
 * retrieves the price of a downloadable product after purchase
 * this price includes any necessary discounts that were applied.
 *
 * @access      public
 * @since       1.0 
 * @param       int $download_id - the ID of the download
 * @param       array $user_purchase_info - an array of all information for the payment
 * @param       string $amount_override a custom amount that over rides the 'edd_price' meta, used for variable prices
 * @return      string - the price of the download
*/
function edd_get_download_final_price($download_id, $user_purchase_info, $amount_override = null)
{
    if (is_null($amount_override)) {
        $original_price = get_post_meta($download_id, 'edd_price', true);
    } else {
        $original_price = $amount_override;
    }
    if (isset($user_purchase_info['discount']) && $user_purchase_info['discount'] != 'none') {
        $price = edd_get_discounted_amount($user_purchase_info['discount'], $original_price);
    } else {
        $price = $original_price;
    }
    return $price;
}
/**
 * Retrieves the final price of a downloadable product after purchase
 * this price includes any necessary discounts that were applied
 *
 * @since 1.0
 * @param int $download_id ID of the download
 * @param array $user_purchase_info - an array of all information for the payment
 * @param string $amount_override a custom amount that over rides the 'edd_price' meta, used for variable prices
 * @return string - the price of the download
 */
function edd_get_download_final_price($download_id = 0, $user_purchase_info, $amount_override = null)
{
    if (is_null($amount_override)) {
        $original_price = get_post_meta($download_id, 'edd_price', true);
    } else {
        $original_price = $amount_override;
    }
    if (isset($user_purchase_info['discount']) && $user_purchase_info['discount'] != 'none') {
        // if the discount was a %, we modify the amount. Flat rate discounts are ignored
        if (edd_get_discount_type(edd_get_discount_id_by_code($user_purchase_info['discount'])) != 'flat') {
            $price = edd_get_discounted_amount($user_purchase_info['discount'], $original_price);
        } else {
            $price = $original_price;
        }
    } else {
        $price = $original_price;
    }
    return apply_filters('edd_final_price', $price, $download_id, $user_purchase_info);
}
/**
 * Get Cart Amount
 *
 * @since 1.0
 * @deprecated 1.9
 * @param bool $add_taxes Whether to apply taxes (if enabled) (default: true)
 * @param bool $local_override Force the local opt-in param - used for when not reading $_POST (default: false)
 * @return float Total amount
*/
function edd_get_cart_amount($add_taxes = true, $local_override = false)
{
    $backtrace = debug_backtrace();
    _edd_deprecated_function(__FUNCTION__, '1.9', 'edd_get_cart_subtotal() or edd_get_cart_total()', $backtrace);
    $amount = edd_get_cart_subtotal();
    if (!empty($_POST['edd-discount']) || edd_get_cart_discounts() !== false) {
        // Retrieve the discount stored in cookies
        $discounts = edd_get_cart_discounts();
        // Check for a posted discount
        $posted_discount = isset($_POST['edd-discount']) ? trim($_POST['edd-discount']) : '';
        if ($posted_discount && !in_array($posted_discount, $discounts)) {
            // This discount hasn't been applied, so apply it
            $amount = edd_get_discounted_amount($posted_discount, $amount);
        }
        if (!empty($discounts)) {
            // Apply the discounted amount from discounts already applied
            $amount -= edd_get_cart_discounted_amount();
        }
    }
    if (edd_use_taxes() && edd_is_cart_taxed() && $add_taxes) {
        $tax = edd_get_cart_tax();
        $amount += $tax;
    }
    if ($amount < 0) {
        $amount = 0.0;
    }
    return apply_filters('edd_get_cart_amount', $amount, $add_taxes, $local_override);
}
/**
 * AJAX Validate Discount
 *
 * Validates the supplied discount.
 *
 * @access      private
 * @since       1.0
 * @return      string
*/
function edd_ajax_validate_discount()
{
    if (isset($_POST['code']) && check_ajax_referer('edd_ajax_nonce', 'nonce')) {
        $user = isset($_POST['user']) ? $_POST['user'] : $_POST['email'];
        $return = array('msg' => '', 'code' => $_POST['code']);
        if (edd_is_discount_used($_POST['code'], $user)) {
            // Called twice if discount is not used (again by edd_is_discount_valid) but allows for beter usr msg and less execution if discount is used.
            $return['msg'] = __('This discount code has been used already', 'edd');
        } else {
            if (edd_is_discount_valid($_POST['code'], $user)) {
                $price = edd_get_cart_amount();
                $discounted_price = edd_get_discounted_amount($_POST['code'], $price);
                $return = array('msg' => 'valid', 'amount' => edd_currency_filter(edd_format_amount($discounted_price)), 'code' => $_POST['code']);
            } else {
                $return['msg'] = __('The discount you entered is invalid', 'edd');
            }
        }
        echo json_encode($return);
    }
    die;
}
/**
 * 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 Cart Amount
 *
 * Gets the total price amount in the cart.
 * uses edd_get_cart_contents().
 *
 * @access      public
 * @since       1.0
 * @param 		$add_taxes bool Whether to apply taxes (if enabled)
 * @param 		$local_override bool Force the local opt-in param - used for when not reading $_POST
 * @return      float the total amount
*/
function edd_get_cart_amount($add_taxes = true, $local_override = false)
{
    $amount = edd_get_cart_subtotal();
    if (isset($_POST['edd-discount']) && $_POST['edd-discount'] != '') {
        // discount is validated before this function runs, so no need to check for it
        $amount = edd_get_discounted_amount($_POST['edd-discount'], $amount);
    }
    if (edd_use_taxes() && $add_taxes) {
        if (edd_local_taxes_only() && (isset($_POST['edd_tax_opt_in']) || $local_override)) {
            // add the tax amount for a local resident
            $tax = edd_get_cart_tax();
            $amount += $tax;
        } elseif (!edd_local_taxes_only()) {
            // add the global tax amount
            $tax = edd_get_cart_tax();
            $amount += $tax;
        }
    }
    return apply_filters('edd_get_cart_amount', $amount, $add_taxes, $local_override);
}
/**
 * Get Cart Amount
 *
 * Gets the total price amount in the cart.
 * uses edd_get_cart_contents().
 *
 * @access      public
 * @since       1.0
 * @return      string - the name of the price option
*/
function edd_get_cart_amount()
{
    $cart_items = edd_get_cart_contents();
    $amount = 0;
    if ($cart_items) {
        foreach ($cart_items as $item) {
            $item_price = edd_get_cart_item_price($item['id'], $item['options']);
            $amount = $amount + $item_price;
        }
        if (isset($_POST['edd-discount']) && $_POST['edd-discount'] != '') {
            // discount is validated before this function runs, so no need to check for it
            $amount = edd_get_discounted_amount($_POST['edd-discount'], $amount);
        }
        return $amount;
    }
    return 0;
}
Example #8
0
/**
 * Sets the discount amount based on the upgrade fee
 *
 * @param $discount float The discount amount
 * @param $item array the cart item array
 * @return float
 */
function affwp_cart_item_discounted_amount($discounted_price, $discounts, $item, $price)
{
    if (!function_exists('EDD')) {
        return $discounted_price;
    }
    if (!EDD()->session->get('is_upgrade')) {
        return $discounted_price;
    }
    $price_id = EDD()->session->get('upgrade_price_id');
    $upgrade_discount = EDD()->session->get('upgrade_discount');
    $cart_discounts = edd_get_cart_discounts();
    if ($upgrade_discount && edd_cart_has_discounts()) {
        $discounted_price = $price - $upgrade_discount;
        foreach ($cart_discounts as $discount) {
            $discounted_price = edd_get_discounted_amount($discount, $discounted_price);
        }
    }
    return $discounted_price;
}
/**
 * Get Cart Amount
 *
 * @since 1.0
 * @param bool $add_taxes Whether to apply taxes (if enabled) (default: true)
 * @param bool $local_override Force the local opt-in param - used for when not reading $_POST (default: false)
 * @return float Total amount
*/
function edd_get_cart_amount($add_taxes = true, $local_override = false)
{
    $amount = edd_get_cart_subtotal(false);
    if (!empty($_POST['edd-discount']) || edd_get_cart_discounts() !== false) {
        // Retrieve the discount stored in cookies
        $discounts = edd_get_cart_discounts();
        // Check for a posted discount
        $posted_discount = isset($_POST['edd-discount']) ? trim($_POST['edd-discount']) : '';
        if ($posted_discount && !in_array($posted_discount, $discounts)) {
            // This discount hasn't been applied, so apply it
            $amount = edd_get_discounted_amount($posted_discount, $amount);
        }
        if (!empty($discounts)) {
            // Apply the discounted amount from discounts already applied
            $amount -= edd_get_cart_discounted_amount();
        }
    }
    if (edd_use_taxes() && $add_taxes) {
        if (edd_local_taxes_only() && (isset($_POST['edd_tax_opt_in']) || $local_override)) {
            // Add the tax amount for a local resident
            $tax = edd_get_cart_tax();
            $amount += $tax;
        } elseif (!edd_local_taxes_only()) {
            // Add the global tax amount
            $tax = edd_get_cart_tax();
            $amount += $tax;
        }
    }
    return apply_filters('edd_get_cart_amount', $amount, $add_taxes, $local_override);
}
/**
 * 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));
}
/**
 * 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;
}