• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Easy Digital Downloads a2z

Easy Digital Downloads a2z

Easy Digital Downloads a2z

  • Home
  • Plugins
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / EDD_Payment_Stats::get_earnings() – Retrieve earning stats

You appear to be a bot. Output may be restricted

Description

Retrieve earning stats

Usage

$float|int = EDD_Payment_Stats::get_earnings( $download_id, $start_date, $end_date, $include_taxes );

Parameters

$download_id
( int ) optional – The download product to retrieve stats for. If false, gets stats for all products
$start_date
( string|bool ) optional – The starting date for which we'd like to filter our sale stats. If false, we'll use the default start date of this_month
$end_date
( string|bool ) optional – The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of this_month
$include_taxes
( bool ) optional default: 1 – If taxes should be included in the earnings graphs

Returns

float|int Total amount of sales based on the passed arguments.

Source

File name: easy-digital-downloads/includes/payments/class-payment-stats.php
Lines:

1 to 100 of 148
  public function get_earnings( $download_id = 0, $start_date = false, $end_date = false, $include_taxes = true ) {
    global $wpdb;

    $this->setup_dates( $start_date, $end_date );

    // Make sure start date is valid
    if ( is_wp_error( $this->start_date ) ) {
      return $this->start_date;
    }

    // Make sure end date is valid
    if ( is_wp_error( $this->end_date ) ) {
      return $this->end_date;
    }

    add_filter( 'posts_where', array( $this, 'payments_where' ) );

    if ( empty( $download_id ) ) {
      // Global earning stats
      $args = array(
        'post_type'              => 'edd_payment',
        'nopaging'               => true,
        'post_status'            => array( 'publish', 'revoked' ),
        'fields'                 => 'ids',
        'update_post_term_cache' => false,
        'suppress_filters'       => false,
        'start_date'             => $this->start_date, // These dates are not valid query args, but they are used for cache keys
        'end_date'               => $this->end_date,
        'edd_transient_type'     => 'edd_earnings', // This is not a valid query arg, but is used for cache keying
        'include_taxes'          => $include_taxes,
      );

      $args   = apply_filters( 'edd_stats_earnings_args', $args );
      $cached = get_transient( 'edd_stats_earnings' );
      $key    = md5( json_encode( $args ) );

      if ( ! isset( $cached[ $key ] ) ) {
        $sales    = get_posts( $args );
        $earnings = 0;

        if ( $sales ) {
          $sales = implode( ',', array_map('intval', $sales ) );

          $total_earnings = $wpdb->get_var( "SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_total' AND post_id IN ({$sales})" );
          $total_tax      = 0;

          if ( ! $include_taxes ) {
            $total_tax = $wpdb->get_var( "SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_tax' AND post_id IN ({$sales})" );
          }

          $total_earnings = apply_filters( 'edd_payment_stats_earnings_total', $total_earnings, $sales, $args );

          $earnings += ( $total_earnings - $total_tax );
        }

        // Cache the results for one hour
        $cached[ $key ] = $earnings;
        set_transient( 'edd_stats_earnings', $cached, HOUR_IN_SECONDS );
      }
    } else {
      // Download specific earning stats
      global $edd_logs, $wpdb;

      $args = array(
        'post_parent'        => $download_id,
        'nopaging'           => true,
        'log_type'           => 'sale',
        'fields'             => 'ids',
        'suppress_filters'   => false,
        'start_date'         => $this->start_date, // These dates are not valid query args, but they are used for cache keys
        'end_date'           => $this->end_date,
        'edd_transient_type' => 'edd_earnings', // This is not a valid query arg, but is used for cache keying
        'include_taxes'      => $include_taxes,
      );

      $args   = apply_filters( 'edd_stats_earnings_args', $args );
      $cached = get_transient( 'edd_stats_earnings' );
      $key    = md5( json_encode( $args ) );

      if ( ! isset( $cached[ $key ] ) ) {
        $this->timestamp = false;
        $log_ids  = $edd_logs->get_connected_logs( $args, 'sale' );

        $earnings = 0;

        if ( $log_ids ) {
          $log_ids     = implode( ',', array_map('intval', $log_ids ) );
          $payment_ids = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '_edd_log_payment_id' AND post_id IN ($log_ids);" );

          foreach ( $payment_ids as $payment_id ) {

            $payment = new EDD_Payment( $payment_id );
            $items   = edd_get_payment_meta_cart_details( $payment_id );

            foreach ( $items as $cart_key => $item ) {

              if ( $item['id'] != $download_id ) {
                continue;
              }

 
[1] [2] Next »

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 8th April 2020

Primary Sidebar

Information

Function name: EDD_Payment_Stats::get_earnings
Class ref: EDD_Payment_Stats
Plugin ref: Easy Digital Downloads
Version: 2.11.6
Sourcefile: includes/payments/class-payment-stats.php
File ref: includes/payments/class-payment-stats.php
Deprecated?: No
API Letters: E,G,P,S

Footer

Easy Digital Downloads a2z
Easy Digital Downloads a2z
Easy Digital Downloads a2z
WordPress 6.0.1
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  edd.wp-a2z.org
© Copyright Easy Digital Downloads a2z 2014-2022. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins