Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save acanza/7673d696e095987b7a5b to your computer and use it in GitHub Desktop.
Save acanza/7673d696e095987b7a5b to your computer and use it in GitHub Desktop.
//* Add spent column to the WordPress Users Table
add_filter( 'manage_users_columns', 'add_customer_spent_column' );
function add_customer_spent_column($columns) {
$columns[ 'customer_spent' ] = __( 'Money Spent', 'woocommerce' );
return $columns;
}
add_action( 'manage_users_custom_column', 'show_customer_spent_column_content', 10, 3 );
function show_customer_spent_column_content( $value, $column_name, $user_id ) {
$customer_spent = wc_price( wc_get_customer_total_spent( $user_id ) );
if ( 'customer_spent' == $column_name ){
$value = $customer_spent;
}
return $value;
}
function user_sortable_columns( $columns ) {
$columns['customer_spent'] = 'customer_spent';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' );
function money_spent_column_orderby( $vars ) {
if ( isset( $vars->query_vars['orderby'] ) && 'customer_spent' == $vars->query_vars['orderby'] ) {
global $wpdb;
$vars->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";
$vars->query_where .= " AND alias.meta_key = '_money_spent' ";
$vars->query_orderby = " ORDER BY alias.meta_value ".($vars->query_vars[ 'order' ] == "ASC" ? "asc " : "desc ");
}
return $vars;
}
add_filter( 'pre_user_query', 'money_spent_column_orderby', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment