Recent WooCommerce Orders in WordPress Multisite Network List

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.
				
					<?php
add_filter( 'wpmu_blogs_columns', 'recent_order_col' );

/**
 * Add Recent Order column to the Network Sites table.
 *
 * @param array $sites_columns
 * 
 * @return array $new_columns
 */
function recent_order_col( $sites_columns ) {
    $new_columns = [];

    foreach ( $sites_columns as $key => $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 '<a href="' . esc_url( get_admin_url( $blog_id ) . 'admin.php?page=wc-orders' ) . '" target="_blank">' . esc_html( $order->get_date_created()->format( 'm/d/Y H:i:s' ) ) . '</a>';
            }
        } 

        restore_current_blog();
    }
}
?>
				
			

If this code helped you, or you have any questions on implementation, don’t hesitate to reach out.