• 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_by_range() – Retrieve sales stats based on range provided (used for Reporting)

You appear to be a bot. Output may be restricted

Description

Retrieve sales stats based on range provided (used for Reporting)

Usage

$array = EDD_Payment_Stats::get_earnings_by_range( $range, $day_by_day, $start_date, $end_date, $include_taxes );

Parameters

$range
( mixed ) optional default: today –
$day_by_day
( mixed ) optional –
$start_date
( string|bool ) optional – The starting date for which we'd like to filter our earnings 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 earnings 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

array Total amount of earnings based on the passed arguments.

Source

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

1 to 77 of 77
  public function get_earnings_by_range( $range = 'today', $day_by_day = false, $start_date = false, $end_date = false, $include_taxes = true ) {
    global $wpdb;

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

    $this->end_date = strtotime( 'midnight', $this->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;
    }

    $earnings = array();

    $cached = get_transient( 'edd_stats_earnings' );
    $key    = md5( $range . '_' . date( 'Y-m-d', $this->start_date ) . '_' . date( 'Y-m-d', strtotime( '+1 DAY', $this->end_date ) ) );
    $sales  = isset( $cached[ $key ] ) ? $cached[ $key ] : false;

    if ( false === $sales || ! $this->is_cacheable( $range ) ) {
      if ( ! $day_by_day ) {
        $select = "DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y, COUNT(DISTINCT posts.ID) as count";
        $grouping = "YEAR(posts.post_date), MONTH(posts.post_date)";
      } else {
        if ( $range == 'today' || $range == 'yesterday' ) {
          $select = "DATE_FORMAT(posts.post_date, '%%d') AS d, DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y, HOUR(posts.post_date) AS h";
          $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date), HOUR(posts.post_date)";
        } else {
          $select = "DATE_FORMAT(posts.post_date, '%%d') AS d, DATE_FORMAT(posts.post_date, '%%m') AS m, YEAR(posts.post_date) AS y";
          $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)";
        }
      }

      if ( $range == 'today' || $range == 'yesterday' ) {
        $grouping = "YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date), HOUR(posts.post_date)";
      }

      $statuses = apply_filters( 'edd_payment_stats_post_statuses', array( 'publish', 'revoked' ) );
      $statuses = "'" . implode( "', '", $statuses ) . "'";

      $earnings = $wpdb->get_results( $wpdb->prepare(
        "SELECT SUM(meta_value) AS total, $select
				 FROM {$wpdb->posts} AS posts
				 INNER JOIN {$wpdb->postmeta} ON posts.ID = {$wpdb->postmeta}.post_ID
				 WHERE posts.post_type IN ('edd_payment')
				 AND {$wpdb->postmeta}.meta_key = '_edd_payment_total'
				 AND posts.post_date >= %s
				 AND posts.post_date < %s
				 AND posts.post_status IN ($statuses)
				 GROUP BY $grouping
				 ORDER by posts.post_date ASC", date( 'Y-m-d', $this->start_date ), date( 'Y-m-d', strtotime( '+1 day', $this->end_date ) ) ), ARRAY_A );

      if ( ! $include_taxes ) {
        $taxes = $wpdb->get_results( $wpdb->prepare(
          "SELECT SUM(meta_value) AS tax, $select
					 FROM {$wpdb->posts} AS posts
					 INNER JOIN {$wpdb->postmeta} ON posts.ID = {$wpdb->postmeta}.post_ID
					 WHERE posts.post_type IN ('edd_payment')
					 AND {$wpdb->postmeta}.meta_key = '_edd_payment_tax'
					 AND posts.post_date >= %s
					 AND posts.post_date < %s
					 AND posts.post_status IN ($statuses)
					 GROUP BY $grouping
					 ORDER by posts.post_date ASC", date( 'Y-m-d', $this->start_date ), date( 'Y-m-d', strtotime( '+1 day', $this->end_date ) ) ), ARRAY_A );

        foreach ( $earnings as $key => $value ) {
          $earnings[ $key ]['total'] -= $taxes[ $key ]['tax'];
        }
      }

      return $earnings;
    }
  }
 

 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_by_range
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 5.9.3
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

  • Home
  • Blog
  • Sitemap
  • Sites