Last active
October 29, 2016 00:50
-
-
Save acanza/7673d696e095987b7a5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* 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