Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active May 26, 2016 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/0791b79d056115f9a2ff683cc5976003 to your computer and use it in GitHub Desktop.
Save Shelob9/0791b79d056115f9a2ff683cc5976003 to your computer and use it in GitHub Desktop.
Get total number and average for sales that are not free sales in Easy Digital Downloads. Prints on an admin page to prevent these queries from running all the time.
<?php
add_action( 'init', function(){
if ( current_user_can( 'manage_options' ) ) {
add_action( 'admin_menu', function () {
add_menu_page( 'Sales Average', 'Sales Average', 'manage_options', 'cwp_sales_average', 'cwp_edd_sales_average' );
} );
function cwp_edd_non_free_sales( $before = false , $after = false ) {
global $wpdb;
$sql = "SELECT * FROM $wpdb->posts WHERE `post_type` = \"edd_payment\" AND `post_status` = \"publish\"";
$payment_posts = $wpdb->get_results( $sql, ARRAY_A );
$payment_post_ids = wp_list_pluck( $payment_posts, 'ID' );
$in = "IN( " . implode( ', ', $payment_post_ids ) . ')';
$sql = sprintf( "SELECT * FROM $wpdb->postmeta WHERE `meta_key` LIKE '_edd_payment_total' AND `meta_value` NOT LIKE '0.00' AND `post_id` %s ", $in );
$payment_totals = $wpdb->get_results( $sql, ARRAY_A );
$sum = array_sum( array_column( $payment_totals, 'meta_value' ) );
$total = count( $payment_totals );
$average = round( $sum / $total, 2, PHP_ROUND_HALF_UP );
return [ 'sum' => $sum, 'total' => $total, 'average' => $average ];
}
function cwp_edd_sales_average(){
extract( cwp_edd_non_free_sales() );
printf( '<div><p>Sum: $<span>%s</span></p><p>Total: %s</p><p>Average: $<span>%s</span></p>', $sum, $total, $average );
}
}
});
<?php
/**
* Plugin Name: EDD Average Earnings Reports
* Plugin URI: https://calderawp.com
* Description:
* Version: 0.1.0
* Author: Josh Pollock for CalderaWP LLC
* Author URI: https://CalderaWP.com
* License: GPLv2+
*/
/**
* Copyright (c) 2016 Josh Pollock for CalderaWP LLC (email : Josh@CalderaWP.com) for CalderaWP LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action( 'init', function() {
if ( is_admin() && current_user_can( 'manage_options' ) ) {
/**
* Make admin page
*/
add_action( 'admin_menu', function () {
add_menu_page( 'Sales Average', 'Sales Average', 'manage_options', 'cwp_edd_sales_average', 'cwp_edd_sales_average' );
} );
/**
* Add Script
*/
add_action( 'admin_enqueue_scripts', function ( $hook ) {
if ( 'toplevel_page_cwp_edd_sales_average' == $hook ) {
wp_enqueue_script( 'chartjs', '//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.min.js' );
}
} );
/**
* Callback for menu page
*/
function cwp_edd_sales_average() {
?>
<canvas id="cwp-edd-sales-average" width="400" height="400"></canvas>
<script>
var sales_data = <?php echo wp_json_encode( cwp_edd_earnings_graph_data() ); ?>;
var data = [];
var labels = [];
var i;
for ( i = 0; i < sales_data.length; i++ ) {
data.push( sales_data[ i ][ 'average' ] );
labels.push( sales_data[ i ][ 'month' ] );
}
var ctx = document.getElementById( "cwp-edd-sales-average" );
var myChart = new Chart( ctx, {
type: 'line',
data: {
labels: labels,
datasets: [ {
label: 'Average price per sale',
data: data
} ]
},
options: {
scales: {
yAxes: [ {
ticks: {
beginAtZero: true
}
} ]
}
}
} );
</script>
<?php
}
/**
* Get stats for a date range
*
* @param bool|string $start
* @param bool|string $end
*
* @return array
*/
function cwp_edd_get_stats( $start = false, $end = false ) {
add_filter( 'edd_stats_earnings_args', 'cwp_edd_filter_stats' );
$stats = new EDD_Payment_Stats();
$earnings = $stats->get_earnings( 0, $start, $end );
$sales = $stats->get_sales( 0, $start, $end );
remove_filter( 'edd_stats_earnings_args', 'cwp_edd_filter_stats' );
return array(
'earnings' => $earnings,
'sales' => $sales,
);
}
/**
* Add filter to remove $0 sales
*
* @uses "edd_stats_earnings_args" action
*
* @param array $args
*
* @return array
*/
function cwp_edd_filter_stats( $args ) {
$args[ 'meta_compare' ] = '!=';
$args[ 'meta_key' ] = '_edd_payment_total';
$args[ 'meta_value' ] = '0.00';
$args = array_reverse( $args );
return $args;
}
/**
* Get data for the graph
*
* @return array
*/
function cwp_edd_earnings_graph_data() {
$sales_data = array();
$months = cwp_edd_last_12_months();
foreach ( $months as $month ) {
$start = $month[ 'start' ];
$end = $month[ 'end' ];
$data = cwp_edd_get_stats( $start, $end );
if ( 0 == $data[ 'sales' ] ) {
$average = 0;
} else {
$average = round( $data[ 'earnings' ] / $data[ 'sales' ], 2 );
}
$sales_data[] = [
'average' => $average,
'earnings' => $data[ 'earnings' ],
'month' => date( 'M, Y', strtotime( $start ) )
];
}
return $sales_data;
}
/**
* Get start/end date for last 12 months
*
* @return array
*/
function cwp_edd_last_12_months() {
$start = ( new DateTime( '1 year ago' ) )->modify( 'first day of this month' );
$end = ( new DateTime() )->modify( 'first day of this month' );
$interval = new DateInterval( 'P1M' );
$period = new DatePeriod( $start, $interval, $end );
$months = array();
$i = 0;
$format = 'Y/m/d';
foreach ( $period as $dt ) {
$end = date( 'Y/m/t', strtotime( $dt->format( $format ) ) );
$months[ $i ][ 'end' ] = $end;
$months[ $i ][ 'start' ] = $dt->format( $format );
$i ++;
}
return $months;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment