/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @return      void
 */
function edd_process_purchase_form()
{
    do_action('edd_pre_process_purchase');
    // Make sure the cart isn't empty
    if (!edd_get_cart_contents() && !edd_cart_has_fees()) {
        $valid_data = false;
        edd_set_error('empty_cart', __('Your cart is empty', 'easy-digital-downloads'));
    } else {
        // Validate the form $_POST data
        $valid_data = edd_purchase_form_validate_fields();
        // Allow themes and plugins to hook to errors
        do_action('edd_checkout_error_checks', $valid_data, $_POST);
    }
    $is_ajax = isset($_POST['edd_ajax']);
    // Process the login form
    if (isset($_POST['edd_login_submit'])) {
        edd_process_purchase_login();
    }
    // Validate the user
    $user = edd_get_purchase_form_user($valid_data);
    if (false === $valid_data || edd_get_errors() || !$user) {
        if ($is_ajax) {
            do_action('edd_ajax_checkout_errors');
            edd_die();
        } else {
            return false;
        }
    }
    if ($is_ajax) {
        echo 'success';
        edd_die();
    }
    // Setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount'], 'address' => $user['address']);
    $auth_key = defined('AUTH_KEY') ? AUTH_KEY : '';
    // Setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'fees' => edd_get_cart_fees(), 'subtotal' => edd_get_cart_subtotal(), 'discount' => edd_get_cart_discounted_amount(), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_total(), 'purchase_key' => strtolower(md5($user['user_email'] . date('Y-m-d H:i:s') . $auth_key . uniqid('edd', true))), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s', current_time('timestamp')), 'user_info' => stripslashes_deep($user_info), 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // Add the user data for hooks
    $valid_data['user'] = $user;
    // Allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // If the total amount in the cart is 0, send to the manual gateway. This emulates a free download purchase
    if (!$purchase_data['price']) {
        // Revert to manual
        $purchase_data['gateway'] = 'manual';
        $_POST['edd-gateway'] = 'manual';
    }
    // Allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // Setup the data we're storing in the purchase session
    $session_data = $purchase_data;
    // Make sure credit card numbers are never stored in sessions
    unset($session_data['card_info']['card_number']);
    // Used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
    edd_set_purchase_session($session_data);
    // Send info to the gateway for payment processing
    edd_send_to_gateway($purchase_data['gateway'], $purchase_data);
    edd_die();
}
/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
*/
function edd_process_purchase_form()
{
    // no need to run on admin
    if (is_admin()) {
        return;
    }
    // verify the nonce for this action
    if (!isset($_POST['edd-nonce']) || !wp_verify_nonce($_POST['edd-nonce'], 'edd-purchase-nonce')) {
        return;
    }
    // make sure the cart isn't empty
    $cart = edd_get_cart_contents();
    if (empty($cart)) {
        wp_die(sprintf(__('Your cart is empty, please return to the %ssite%s and try again.', 'edd'), '<a href="' . esc_url(home_url()) . '" title="' . get_bloginfo('name') . '">', '</a>'), __('Error', 'edd'));
    }
    // validate the form $_POST data
    $valid_data = edd_purchase_form_validate_fields();
    // allow themes and plugins to hoook to errors
    do_action('edd_checkout_error_checks', $_POST);
    // check errors
    if (false !== ($errors = edd_get_errors())) {
        // we have errors, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // check user
    if (false === ($user = edd_get_purchase_form_user($valid_data))) {
        // something went wrong when collecting data, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'subtotal' => edd_get_cart_amount(false), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_amount(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // add the user data for hooks
    $valid_data['user'] = $user;
    // allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // if the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if ($purchase_data['price'] <= 0) {
        // revert to manual
        $valid_data['gateway'] = 'manual';
    }
    // used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
    edd_set_purchase_session($purchase_data);
    // send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    exit;
}
/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
*/
function edd_process_purchase_form()
{
    global $edd_options;
    // no need to run on admin
    if (is_admin()) {
        return;
    }
    // verify the nonce for this action
    if (!isset($_POST['edd-nonce']) || !wp_verify_nonce($_POST['edd-nonce'], 'edd-purchase-nonce')) {
        return;
    }
    // validate the form $_POST data
    $valid_data = edd_purchase_form_validate_fields();
    // allow themes and plugins to hoook to errors
    do_action('edd_checkout_error_checks', $_POST);
    // check errors
    if (false !== ($errors = edd_get_errors())) {
        // we have errors, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // check user
    if (false === ($user = edd_get_purchase_form_user($valid_data))) {
        // something went wrong when collecting data, send back to checkout
        edd_send_back_to_checkout('?payment-mode=' . $valid_data['gateway']);
        exit;
    }
    // setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'price' => edd_get_cart_amount(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // add the user data for hooks
    $valid_data['user'] = $user;
    // allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // if the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if ($purchase_data['price'] <= 0) {
        // revert to manual
        $valid_data['gateway'] = 'manual';
    }
    if (isset($edd_options['show_links_on_success'])) {
        // used for showing download links to non logged-in users after purchase
        edd_set_purchase_session($purchase_data);
    }
    // send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    exit;
}
/**
 * Process Purchase Form
 *
 * Handles the purchase form process.
 *
 * @access      private
 * @since       1.0
 * @version     1.0.8.1
 * @return      void
 */
function edd_process_purchase_form()
{
    // Make sure the cart isn't empty
    if (!edd_get_cart_contents()) {
        edd_set_error('empty_cart', __('Your cart is empty', 'edd'));
    } else {
        // Validate the form $_POST data
        $valid_data = edd_purchase_form_validate_fields();
        // Allow themes and plugins to hoook to errors
        do_action('edd_checkout_error_checks', $valid_data, $_POST);
    }
    $is_ajax = isset($_POST['edd_ajax']);
    $user = edd_get_purchase_form_user($valid_data);
    if (edd_get_errors() || !$user) {
        if ($is_ajax) {
            do_action('edd_ajax_checkout_errors');
            edd_die();
        } else {
            return false;
        }
    }
    if ($is_ajax) {
        echo 'success';
        edd_die();
    }
    // Setup user information
    $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount']);
    // Setup purchase information
    $purchase_data = array('downloads' => edd_get_cart_contents(), 'fees' => edd_get_cart_fees(), 'subtotal' => edd_get_cart_subtotal(), 'discount' => edd_get_cart_discounted_amount(), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_total(), 'purchase_key' => strtolower(md5(uniqid())), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s'), 'user_info' => $user_info, 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']);
    // Add the user data for hooks
    $valid_data['user'] = $user;
    // Allow themes and plugins to hook before the gateway
    do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data);
    // Allow the purchase data to be modified before it is sent to the gateway
    $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data);
    // If the total amount in the cart is 0, send to the manaul gateway. This emulates a free download purchase
    if (!$purchase_data['price']) {
        // Revert to manual
        $valid_data['gateway'] = 'manual';
    }
    // Used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data.
    edd_set_purchase_session($purchase_data);
    // Send info to the gateway for payment processing
    edd_send_to_gateway($valid_data['gateway'], $purchase_data);
    edd_die();
}
/**
 * Gets the total tax amount for the cart contents
 *
 * @since 1.2.3
 *
 * @return mixed|void Total tax amount
 */
function edd_get_cart_tax()
{
    $cart_tax = 0;
    $items = edd_get_cart_content_details();
    if ($items) {
        $taxes = wp_list_pluck($items, 'tax');
        if (is_array($taxes)) {
            $cart_tax = array_sum($taxes);
        }
    }
    $cart_tax += edd_get_cart_fee_tax();
    return apply_filters('edd_get_cart_tax', edd_sanitize_amount($cart_tax));
}
/**
 * Retrieves the total discounted amount on the cart
 *
 * @since 1.4.1
 *
 * @param bool $discounts Discount codes
 *
 * @return float|mixed|void Total discounted amount
 */
function edd_get_cart_discounted_amount($discounts = false)
{
    $amount = 0.0;
    $items = edd_get_cart_content_details();
    if ($items) {
        $discounts = wp_list_pluck($items, 'discount');
        if (is_array($discounts)) {
            $amount = array_sum($discounts);
        }
    }
    return apply_filters('edd_get_cart_discounted_amount', $amount);
}
<?php

namespace Yoast\YoastCom\Theme;

$cart_item = $template_args['cart_item'];
$percentage = $template_args['percentage'];
$cart_items = edd_get_cart_content_details();
$discount_amount = 0;
foreach ($cart_items as $item) {
    $current_discount = $item['discount'];
    $discount_amount += edd_sl_cart_details_item_discount($current_discount, $item) - $current_discount;
}
$discount_amount = edd_currency_filter(edd_format_amount(-1 * $discount_amount));
?>
<li class="grid edd_cart_item item-discount">
	<div class="three-seventh offset-three-seventh">
		<?php 
printf(__('License renewal discount: %s', 'edd_sl'), $percentage . '%');
?>
	</div>
	<div class="one-seventh">
		<?php 
echo esc_html($discount_amount);
?>
	</div>
</li>
<?php 
 /**
  * Apply the discounts to the cart
  *
  * @since 1.0
  *
  * @access public
  * @return void
  */
 public function apply_discounts($download_id, $options)
 {
     if (!function_exists('rcp_is_active')) {
         return;
     }
     $user_id = get_current_user_id();
     if (!rcp_is_active($user_id)) {
         return;
     }
     $sub_id = rcp_get_subscription_id($user_id);
     if (!$sub_id) {
         $this->clear_cart_discounts();
         return;
     }
     // Check for member discounts
     $discounts = get_posts(array('post_type' => 'edd_rcp_discount', 'posts_per_page' => '1', 'fields' => 'ids', 'meta_query' => array('relation' => 'AND', array('key' => '_edd_rcp_discount_subscription', 'value' => $sub_id))));
     if (!$discounts) {
         $this->clear_cart_discounts();
         return;
     }
     // Get cart details
     $cart_amount = edd_get_cart_subtotal();
     $cart_details = edd_get_cart_content_details();
     if (empty($cart_details)) {
         $this->clear_cart_discounts();
         return;
     }
     // Subtract exclusions from the cart amount before calculating the discount below.
     foreach ($cart_details as $key => $download) {
         // Check for product-level exclusion
         if (get_post_meta($download['id'], 'rcp_member_discount_exclude')) {
             $cart_amount -= $download['item_price'];
             continue;
         }
         // Check for download category exclusions
         $terms = wp_get_object_terms($download['id'], 'download_category');
         if (!$terms) {
             continue;
         }
         $term_discounted = false;
         foreach ($terms as $term) {
             if (!$term_discounted && get_term_meta($term->term_id, 'rcp_member_discount_exclude')) {
                 $cart_amount -= $download['item_price'];
                 $term_discounted = true;
                 continue;
             }
         }
     }
     // If the cart amount is 0 after subtracting exclusions, no member discount is applied.
     if ('0' == $cart_amount) {
         $this->clear_cart_discounts();
         return;
     }
     foreach ($discounts as $discount) {
         $percent = get_post_meta($discount, '_edd_rcp_discount_amount', true);
         $amount = $cart_amount * ($percent / 100) * -1;
         EDD()->fees->add_fee(array('amount' => $amount, 'label' => get_the_title($discount), 'id' => 'rcp_member_discount'));
         EDD()->session->set('rcp_member_discount_id', $discount);
     }
 }
Exemplo n.º 9
0
/**
 * Displays the renewal discount row on the cart
 *
 * @since 3.0.2
 * @return void
 */
function edd_sl_cart_items_renewal_row()
{
    if (!edd_sl_renewals_allowed()) {
        return;
    }
    if (!EDD()->session->get('edd_is_renewal')) {
        return;
    }
    // bail early if a renewal discount is not set (or set at 0)
    $renewal_discount = edd_sl_get_renewal_discount_percentage();
    if (empty($renewal_discount)) {
        return;
    }
    $cart_items = edd_get_cart_content_details();
    $renewals = edd_sl_get_renewal_keys();
    $discount_amount = 0;
    foreach ($cart_items as $key => $item) {
        if (!isset($renewals[$key])) {
            continue;
        }
        $discount_amount += edd_sl_get_renewal_discount_amount($item, $renewals[$key]);
    }
    $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_sl'), $renewal_discount . '%', $discount_amount);
    ?>
</td>
	</tr>
<?php 
}