WooCommerce: View Customer Order History on Single Order Admin Page

Viewing the WooCommerce customer’s order history directly on the single order admin page can be incredibly useful when you need quick access to a customer’s past purchases without navigating away from the current order screen.

With a simple PHP snippet, you can add this functionality, enabling you to view previous orders and better understand customer behavior in real time.

This customization not only enhances your workflow but also improves customer service, allowing you to address inquiries more efficiently.

Below, I’ll walk through the steps needed to implement this feature, providing you with the PHP code necessary to display order history details on the order admin page itself.

As always, remember to test customizations in a staging environment to ensure compatibility with your setup!

If you open a single order from the admin, you will now find this new “meta box”, with the latest 10 orders from the current WooCommerce customer ID

PHP Snippet: Print Customer’s Last 10 Orders @ WooCommerce Single Order Admin Page

A few notes:

Hook Creation (add_meta_boxes): This hook adds a meta box to the WooCommerce single order admin page. The add_meta_box function sets the title to “Customer Order History” and associates it with the shop_order post type, so it appears specifically on WooCommerce order pages.

Callback Function (bbloomer_display_order_history): This function is triggered when the meta box is loaded. It gathers relevant customer order history and outputs it in a table format.

Order Retrieval: The code fetches the current order’s customer ID to load up to 10 previous orders for that customer. By using 'return' => 'ids', it retrieves only order IDs, making the query faster.

Output Formatting: For each past order, the code displays the order ID, date, product names (separated by |), and status, providing an organized view of the customer’s order history on the single order admin page.

/**
 * @snippet       Orders History @ WooCommerce Single Order Admin Page
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'add_meta_boxes', function() {
    add_meta_box( 'order_history', 'Customer Order History', 'bbloomer_display_order_history', 'shop_order', 'normal', 'default' );
}, 1 );
 
function bbloomer_display_order_history() {
	global $post;
	$order = wc_get_order( $post->ID );
	if ( ! $order ) return;
   $orders = array();
	if ( $id = $order->get_customer_id() ) {
		$orders = wc_get_orders( [ 'customer_id' => $id, 'return' => 'ids', 'limit' => 10 ] );
	}
	if ( ! $orders ) return;
	echo '<table style="width:100%"><thead><tr><th>ID</th><th>DATE</th><th>ITEMS</th><th>STATUS</th></tr></thead><tbody>';
	foreach ( $orders as $order_id ) {
		$order = wc_get_order( $order_id );
		if ( ! $order ) continue;
		$items = array();
		foreach ( $order->get_items() as $item_id => $item ) {
			$items[] = $item->get_name();
		}
		echo '<tr><td>' . $order_id . '</td><td>' . wc_format_datetime( $order->get_date_created() ) . '</td><td>' . implode( ' | ', $items ) . '</td><td>' . $order->get_status() . '</td></tr>';
	}
	echo '</tbody></table>';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

9 thoughts on “WooCommerce: View Customer Order History on Single Order Admin Page

  1. Hi Rodolfo,
    This feature for displaying order history in WordPress admin is fantastic and highly useful for those who want to quickly review this information. Additionally, if it could also display the customer’s full name, email address, and phone number, it would be even better!

    1. Great! I plan to build a mini plugin out of this, will let everyone know. The only problem is with the number of columns, if too many the table will require horizontal scrolling and I’m not sure if that’s good for UX

  2. Hi Rodolfo,
    Nothing happens on the order page so far
    Maybe HPOS raleted?
    Thanks

    1. Not sure, but this is definitely HPOS compatible because I use wc_get_orders() to get orders. Sure there are previous orders from the same customer ID?

  3. Hi Rodolfo,

    Really nice idea for very busy stores. Sadly, I’ve tested the code and nothing happens 🙁

    1. Thank you Fernando! this works perfectly on my dev site + some of the commenters’ websites as well. Are you use there are past orders from the current customer ID?

      1. I’ll check it again, Thank you Rodolfo

        Ciao

  4. Clever idea, thanks. But as it stands not so useful. as it is very busy when there are multiple items in an order, and it is not clickable

    1. Sure, in that case you could print one line per item (so an order with 5 items would become 5 lines). Also, you can make the order id clickable, that’s super easy

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *