Today's Sales Dashboard Widget
Adds a dashboard widget showing today's number of orders, revenue and average order value, so you can check the day at a glance.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'view_woocommerce_reports' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_todays_sales', 'Today\'s sales', function () {
$start = new DateTime( 'today', wp_timezone() );
$orders = wc_get_orders( array(
'status' => array( 'wc-processing', 'wc-completed', 'wc-on-hold' ),
'date_created' => '>=' . $start->getTimestamp(),
'limit' => -1,
'type' => 'shop_order',
) );
$count = count( $orders );
$revenue = 0;
foreach ( $orders as $order ) {
$revenue += (float) $order->get_total() - (float) $order->get_total_refunded();
}
$average = $count ? $revenue / $count : 0;
echo '<table class="widefat striped"><tbody>';
printf( '<tr><td>Orders</td><td><strong>%d</strong></td></tr>', (int) $count );
printf( '<tr><td>Revenue</td><td><strong>%s</strong></td></tr>', wp_kses_post( wc_price( $revenue ) ) );
printf( '<tr><td>Average order</td><td><strong>%s</strong></td></tr>', wp_kses_post( wc_price( $average ) ) );
echo '</tbody></table>';
$hpos = class_exists( '\\Automattic\\WooCommerce\\Utilities\\OrderUtil' ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
printf( '<p><a href="%s">View orders</a></p>', esc_url( admin_url( $hpos ? 'admin.php?page=wc-orders' : 'edit.php?post_type=shop_order' ) ) );
} );
} );
Notes
Counts orders in processing, completed and on-hold status created since midnight in the site timezone.
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.