Success in eCommerce means knowing your customers – and knowing your customers means having data at your fingertips. While working on a recent design refresh for Hunt & Heist, we realized that some really important customer information is strangely left out of the core WooCommerce code – an easy way to see the total number of orders and money an individual customer has spent in your store.
Thanks to some insights provided by Josh Kohlbach and Navjot Singh, we were able to put together a function to add sortable columns for Number of Orders and Total Amount Spent to the Users page in the WordPress backend. How handy is that?
We’re able to see at a glance which customers have made the most orders and spent the most in a WooCommerce store — and conversely, who’s spent the least. You might realize that you’d like to offer products with a limited run to your most loyal customers first or perhaps send a coupon to customers who’ve registered but never placed an order!
All you need to do to easily see this information on your own WooCommerce site is add the following code to your child theme’s functions.php file or your functionality plugin. So get analyzing!
////////////////////////////////////////////////////////////
// additional customer info
// https://razorfrog.com/woocommerce-user-order-data-columns/
////////////////////////////////////////////////////////////
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function add_user_details_columns($columns) {
$columns['user_orders'] = 'Orders';
$columns['user_total_spent'] = 'Total Spent';
return $columns;
}
function show_user_details_column_content($value, $column_name, $user_id) {
if ('user_orders' == $column_name)
return wc_get_customer_order_count($user_id);
else if ('user_total_spent' == $column_name)
return wc_price(wc_get_customer_total_spent($user_id));
return $value;
}
function add_order_details_to_user_list() {
add_filter('manage_users_columns', 'add_user_details_columns');
add_action('manage_users_custom_column', 'show_user_details_column_content', 10, 3);
}
add_action('admin_init', 'add_order_details_to_user_list');
add_filter( 'manage_users_sortable_columns', 'my_sortable_cake_column' );
function my_sortable_cake_column( $columns ) {
$columns['user_orders'] = '_order_count';
$columns['user_total_spent'] = '_money_spent';
return $columns;
}
add_action('pre_user_query', 'status_column_orderby');
function status_column_orderby($userquery){
if('_order_count'==$userquery->query_vars['orderby']) {
global $wpdb;
$userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) "; //note use of alias
$userquery->query_where .= " AND alias.meta_key = '_order_count' "; //which meta are we sorting with?
$userquery->query_orderby = " ORDER BY alias.meta_value + 0 ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
} else if('_money_spent'==$userquery->query_vars['orderby']) {
global $wpdb;
$userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) "; //note use of alias
$userquery->query_where .= " AND alias.meta_key = '_money_spent' "; //which meta are we sorting with?
$userquery->query_orderby = " ORDER BY alias.meta_value + 0 ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
}
}
}
Awesome, thanks!
It’s been two years.. but maybe you will respond. It doesn’t work quite well. I have around 1000 customers and it’s showing me like 120 customers only. The data doesn’t seem to be right either. I switch from Prestashop to WooCommerce, but that was mistake I think. In presta for everyclient I have his total spent value. Here in WooCommerce such simple and obvious functionality seems impossible to get. Thanks for your function.
Hi Lukas – It’s still working fine on our sites. Perhaps there’s an issue with duplicated database tables from Prestashop?
Thanks for the code!