Returning Customer Info on the Order Screen
Shows how many previous orders the customer has placed and their total spend on the order edit screen, so you can recognize loyal buyers.
Opens WPIDE on
Code
add_action( 'woocommerce_admin_order_data_after_order_details', function ( $order ) {
$email = $order->get_billing_email();
if ( ! $email ) {
return;
}
$args = array(
'limit' => -1,
'return' => 'ids',
'type' => 'shop_order',
'status' => array( 'wc-completed', 'wc-processing', 'wc-on-hold' ),
'exclude' => array( $order->get_id() ),
);
if ( $order->get_customer_id() ) {
$args['customer_id'] = $order->get_customer_id();
} else {
$args['billing_email'] = $email;
}
$ids = wc_get_orders( $args );
$spent = 0;
foreach ( $ids as $id ) {
$previous = wc_get_order( $id );
$spent += $previous ? (float) $previous->get_total() : 0;
}
echo '<p class="form-field form-field-wide" style="clear:both;padding-top:8px;">';
if ( $ids ) {
printf( '<strong>Returning customer:</strong> %1$d previous orders, %2$s spent.', count( $ids ), wp_kses_post( wc_price( $spent ) ) );
} else {
echo '<strong>New customer:</strong> first order.';
}
echo '</p>';
} );
Notes
Guest orders are matched by billing email.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.