EDD Earnings This Month vs Last Month Widget
Adds a dashboard widget with this month's Easy Digital Downloads earnings and order count next to last month's totals, using EDD's own stats engine.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! class_exists( '\EDD\Stats' ) || ! current_user_can( 'view_shop_reports' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_edd_month', 'Earnings this month', function () {
$data = get_transient( 'wpide_edd_month_earnings' );
if ( false === $data ) {
$stats = new \EDD\Stats();
$data = array(
'this_earnings' => (float) $stats->get_order_earnings( array( 'range' => 'this_month' ) ),
'this_orders' => (int) $stats->get_order_count( array( 'range' => 'this_month' ) ),
'last_earnings' => (float) $stats->get_order_earnings( array( 'range' => 'last_month' ) ),
'last_orders' => (int) $stats->get_order_count( array( 'range' => 'last_month' ) ),
);
set_transient( 'wpide_edd_month_earnings', $data, 30 * MINUTE_IN_SECONDS );
}
$money = function ( $amount ) {
return esc_html( html_entity_decode( edd_currency_filter( edd_format_amount( $amount ) ) ) );
};
printf(
'<p style="font-size:24px;margin:.2em 0"><strong>%1$s</strong></p><p>%2$s</p><p>%3$s</p>',
$money( $data['this_earnings'] ), // phpcs:ignore WordPress.Security.EscapeOutput -- Escaped in $money.
esc_html( sprintf( '%d orders this month', $data['this_orders'] ) ),
esc_html( sprintf( 'Last month: %1$s from %2$d orders.', html_entity_decode( edd_currency_filter( edd_format_amount( $data['last_earnings'] ) ) ), $data['last_orders'] ) )
);
} );
} );
Notes
Only users who can view shop reports see it, and the figures are cached for 30 minutes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.