Show customer order history in the WooCommerce order detail page

When managing orders in WooCommerce, sometimes you need more context: has this customer ordered before? How often? Do they usually cancel? Having that information visible can help you make better decisions.

In this article, I’ll show you how to add a custom metabox to the order edit screen, displaying all other orders from the same customer, with the most important fields: ID (with link), Order Number, Date, Status, and Total. All of this with a simple snippet—no plugins needed.

 

What we’re building

At the end of this tutorial, when viewing a WooCommerce order placed by a registered user, you’ll see a box like this on the right sidebar:

|----------------------------------------------------------|
| ID    | Order No. | Date       | Status      | Total     |
|-------|-----------|------------|-------------|-----------|
| #123  | 123       | 2024-12-10 | Completed   | $50.00    |
| #100  | 100       | 2024-10-02 | Cancelled   | $30.00    |
| #91   | 91        | 2024-09-15 | Processing  | $75.99    |

 

The snippet

Paste this code into your child theme’s functions.phpfile or into a custom plugin:

ID );
    $customerId = $order->get_customer_id();

    if ( ! $customerId ) {
        echo '

This order has no assigned customer.

'; return; } $customerOrders = wc_get_orders( [ 'customer_id' => $customerId, 'exclude' => [ $order->get_id() ], 'limit' => -1, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'ids' ] ); if ( empty( $customerOrders ) ) { echo '

This customer has no other orders.

'; return; } echo ''; echo ''; foreach ( $customerOrders as $orderId ) { $pastOrder = wc_get_order( $orderId ); $orderLink = get_edit_post_link( $pastOrder->get_id() ); $orderIdHtml = '#' . esc_html( $pastOrder->get_id() ) . ''; $orderNumber = $pastOrder->get_order_number(); $orderDate = $pastOrder->get_date_created() ? $pastOrder->get_date_created()->date( 'Y-m-d' ) : '-'; $orderStatus = wc_get_order_status_name( $pastOrder->get_status() ); $orderTotal = $pastOrder->get_formatted_order_total(); echo ''; } echo '
ID Order No. Date Status Total
' . $orderIdHtml . ' ' . esc_html( $orderNumber ) . ' ' . esc_html( $orderDate ) . ' ' . esc_html( $orderStatus ) . ' ' . $orderTotal . '
'; }

 

Code Explanation

  • add_meta_box: Adds a custom metabox to the order edit screen.
  • get_customer_id(): Retrieves the user ID who placed the current order (if registered).
  • wc_get_orders(): Fetches all other orders from the same customer, excluding the current one.
  • get_order_number(): Returns the actual order number, which may differ from the ID if you’re using plugins like Sequential Order Numbers.
  • get_edit_post_link(): Provides the admin URL to edit the selected order.
  • get_date_created(): Gets the order’s creation date (formatted for display).
  • wc_get_order_status_name(): Converts the internal status slug (completed, cancelled, etc.) to a readable label like “Completed” or “Cancelled”.
  • get_formatted_order_total(): Displays the order total with currency formatting.
  • Simple HTML Table: Keeps the UI clean and compact inside the admin sidebar.

 

Final result

This metabox gives you quick insight into the customer’s behavior—whether they’re a frequent buyer, a new client, or someone who often cancels. A small improvement that can really help in stores where customer history matters (like custom products, returns, or support-heavy shops).

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Back to Top
0
Would love your thoughts, please comment.x
()
x