Razorfrog Web Design Razorfrog Web Design
  • Home
  • About
  • Services
    • Responsive Web Design
    • E-Commerce Stores
    • Logo & Brand Identity
    • Search Engine Optimization
    • Copywriting & Editing
    • Photography & Imaging
  • Portfolio
  • News
  • Contact
Close
  • Home
  • About
  • Services
    • Responsive Web Design
    • E-Commerce Stores
    • Logo & Brand Identity
    • Search Engine Optimization
    • Copywriting & Editing
    • Photography & Imaging
  • Portfolio
  • News
  • Contact

Add WooCommerce Customer Order Info to Users Page with This Code Snippet

August 12, 2016

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
		}
	}
}
Posted in: Code Author: Max Elman

Share

FacebookTwitter

3 Comments

  1. afungusboy
    January 19, 2017 at 7:51 am / Reply

    Awesome, thanks!

  2. Lukas
    November 16, 2019 at 2:27 am / Reply

    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.

    • Max Elman
      November 18, 2019 at 11:08 am /

      Hi Lukas – It’s still working fine on our sites. Perhaps there’s an issue with duplicated database tables from Prestashop?

Leave a Reply / Cancel Reply

  • Prev
  • Next

© 2008-2019 RAZORFROG WEB DESIGN
Sitemap • Terms of Service • Privacy Policy
415-480-4587 • 540 Howard St
, San Francisco, CA 94105
Powered by WordPress

We use cookies to ensure the best possible experience for our visitors. Please visit our privacy policy to review our cookies policy. By clicking "I Accept" or continuing to browse, you accept the use of cookies on this site.I Accept