Ejemplo n.º 1
0
/**
 * Payment actions
 *
 * @since 1.1
*/
function edd_csau_payment_actions($payment_id)
{
    $cart_details = edd_get_payment_meta_cart_details($payment_id);
    if (is_array($cart_details)) {
        // Increase purchase count and earnings
        foreach ($cart_details as $download) {
            // "bundle" or "default"
            $download_type = edd_get_download_type($download['id']);
            $price_id = isset($download['options']['price_id']) ? (int) $download['options']['price_id'] : false;
            // Increase earnings, and fire actions once per quantity number
            for ($i = 0; $i < $download['quantity']; $i++) {
                if (!edd_is_test_mode() || apply_filters('edd_log_test_payment_stats', false)) {
                    if (isset($download['item_number']['cross_sell'])) {
                        $type = 'cross_sell';
                    } elseif (isset($download['item_number']['upsell'])) {
                        $type = 'upsell';
                    } else {
                        $type = null;
                    }
                    if ($type) {
                        edd_csau_increase_purchase_count($download['id'], $type);
                        edd_csau_increase_earnings($download['id'], $download['price'], $type);
                        edd_csau_record_sale_in_log($download['id'], $payment_id, $price_id, $type);
                    }
                }
                $types[] = $type;
                $types = array_unique(array_filter($types));
            }
        }
        // Clear the total earnings cache
        delete_transient('edd_' . $type . '_earnings_total');
    }
    if ($types) {
        foreach ($types as $type) {
            // store the total amount of cross-sell earnings
            update_post_meta($payment_id, '_edd_payment_' . $type . '_total', edd_csau_calculate_sales($payment_id, $type));
            $amount = edd_csau_get_payment_amount($payment_id, $type);
            // increase earnings
            edd_csau_increase_total_earnings($amount, $type);
        }
    }
}
Ejemplo n.º 2
0
/**
 * Get Total Cross-sell/Upsell Earnings
 *
 * @since 1.1
 * @return float $total Total earnings
 */
function edd_csau_get_total_earnings($type)
{
    $total = get_option('edd_' . $type . '_earnings_total', 0);
    // If no total stored in DB, use old method of calculating total earnings
    if (!$total) {
        $total = get_transient('edd_' . $type . '_earnings_total');
        if (false === $total) {
            $total = (double) 0;
            $args = apply_filters('edd_get_total_' . $type . '_earnings_args', array('offset' => 0, 'number' => -1, 'mode' => 'live', 'status' => array('publish', 'revoked'), 'fields' => 'ids'));
            $payments = edd_get_payments($args);
            if ($payments) {
                foreach ($payments as $payment) {
                    $total += edd_csau_get_payment_amount($payment, $type);
                }
            }
            // Cache results for 1 day. This cache is cleared automatically when a payment is made
            set_transient('edd_' . $type . '_earnings_total', $total, 86400);
            // Store the total for the first time
            update_option('edd_' . $type . '_earnings_total', $total);
        }
    }
    if ($total < 0) {
        $total = 0;
        // Don't ever show negative earnings
    }
    return apply_filters('edd_' . $type . '_total_earnings', round($total, 2), $type);
}
 /**
  * Get the Export Data
  *
  * @access public
  * @since 1.4.4
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @return array $data The data for the CSV file
  */
 public function get_data()
 {
     global $wpdb, $edd_options;
     $data = array();
     $payments = edd_get_payments(array('offset' => 0, 'number' => -1, 'mode' => edd_is_test_mode() ? 'test' : 'live', 'status' => isset($_POST['edd_export_payment_status']) ? $_POST['edd_export_payment_status'] : 'any', 'month' => isset($_POST['month']) ? absint($_POST['month']) : date('n'), 'year' => isset($_POST['year']) ? absint($_POST['year']) : date('Y')));
     foreach ($payments as $payment) {
         // skip over payments that don't have upsells in them
         if (!get_post_meta($payment->ID, '_edd_payment_upsell_total', true)) {
             continue;
         }
         $payment_meta = edd_get_payment_meta($payment->ID);
         $user_info = edd_get_payment_meta_user_info($payment->ID);
         $downloads = edd_get_payment_meta_cart_details($payment->ID);
         $total = edd_csau_get_payment_amount($payment->ID, 'upsell');
         $user_id = isset($user_info['id']) && $user_info['id'] != -1 ? $user_info['id'] : $user_info['email'];
         $products = '';
         $skus = '';
         if ($downloads) {
             foreach ($downloads as $key => $download) {
                 // skip over downloads which aren't upsells
                 if (!isset($download['item_number']['upsell'])) {
                     continue;
                 }
                 // Download ID
                 $id = isset($payment_meta['cart_details']) ? $download['id'] : $download;
                 // If the download has variable prices, override the default price
                 $price_override = isset($payment_meta['cart_details']) ? $download['price'] : null;
                 $price = edd_get_download_final_price($id, $user_info, $price_override);
                 // Display the Downoad Name
                 $products .= get_the_title($id) . ' - ';
                 if (edd_use_skus()) {
                     $sku = edd_get_download_sku($id);
                     if (!empty($sku)) {
                         $skus .= $sku;
                     }
                 }
                 if (isset($downloads[$key]['item_number']) && isset($downloads[$key]['item_number']['options'])) {
                     $price_options = $downloads[$key]['item_number']['options'];
                     if (isset($price_options['price_id'])) {
                         $products .= edd_get_price_option_name($id, $price_options['price_id']) . ' - ';
                     }
                 }
                 $products .= html_entity_decode(edd_currency_filter($price));
                 if ($key != count($downloads) - 1) {
                     $products .= ' / ';
                     if (edd_use_skus()) {
                         $skus .= ' / ';
                     }
                 }
             }
         }
         if (is_numeric($user_id)) {
             $user = get_userdata($user_id);
         } else {
             $user = false;
         }
         $data[] = array('id' => $payment->ID, 'email' => $payment_meta['email'], 'first' => $user_info['first_name'], 'last' => $user_info['last_name'], 'products' => $products, 'skus' => $skus, 'amount' => html_entity_decode(edd_format_amount($total)), 'tax' => html_entity_decode(edd_get_payment_tax($payment->ID, $payment_meta)), 'discount' => isset($user_info['discount']) && $user_info['discount'] != 'none' ? $user_info['discount'] : __('none', 'edd'), 'gateway' => edd_get_gateway_admin_label(get_post_meta($payment->ID, '_edd_payment_gateway', true)), 'key' => $payment_meta['key'], 'date' => $payment->post_date, 'user' => $user ? $user->display_name : __('guest', 'edd'), 'status' => edd_get_payment_status($payment, true));
         if (!edd_use_skus()) {
             unset($data['skus']);
         }
     }
     $data = apply_filters('edd_export_get_data', $data);
     $data = apply_filters('edd_export_get_data_' . $this->export_type, $data);
     return $data;
 }