Skip to content

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?

user-columns

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
		}
	}
}

Max Elman is Razorfrog's Founder and Project Manager. He is a tech-savvy internet guru and has led Razorfrog's award-winning web design team since 2008. View Max's bio for more details.

Toggle light/dark mode

Back To Top