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!
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>';
}
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!
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
Hi Rodolfo,
Nothing happens on the order page so far
Maybe HPOS raleted?
Thanks
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?
Hi Rodolfo,
Really nice idea for very busy stores. Sadly, I’ve tested the code and nothing happens 🙁
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?
I’ll check it again, Thank you Rodolfo
Ciao
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
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