Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.