This code enhances the WordPress Multisite Network Admin dashboard by adding a new column to the Sites table that shows the date of the most recent WooCommerce order. The order date links direct to the sites orders page for easy access. It facilitates monitoring of e-commerce activity across multiple sites without the need to visit each site’s dashboard individually.
$label ) {
$new_columns[ $key ] = $label;
// Insert column after lastupdated
if ( 'lastupdated' === $key ) {
$new_columns['recent_order'] = __( 'Recent Order', 'textdomain' );
}
}
// If lastupdated wasn’t found stick it at the end
if ( ! isset( $new_columns['recent_order'] ) ) {
$new_columns['recent_order'] = __( 'Recent Order', 'textdomain' );
}
return $new_columns;
}
add_action( 'manage_sites_custom_column', 'recent_order_col_data', 10, 2 );
/**
* Populate order data in Recent Order column.
*
* @param string $column_name
* @param int $blog_id
*/
function recent_order_col_data( $column_name, $blog_id ) {
if ( $column_name === 'recent_order' ) {
switch_to_blog( $blog_id );
$args = array(
'limit' => 1,
'type' => 'shop_order',
'orderby' => 'date',
'order' => 'DESC',
);
$orders = wc_get_orders( $args );
if ( ! empty( $orders ) ) {
foreach ( $orders as $order ) {
echo '' . esc_html( $order->get_date_created()->format( 'm/d/Y H:i:s' ) ) . '';
}
}
restore_current_blog();
}
}
?>