Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save razorfrog/7b33abfd390771d2c2873e2b27a5374c to your computer and use it in GitHub Desktop.
Save razorfrog/7b33abfd390771d2c2873e2b27a5374c to your computer and use it in GitHub Desktop.
<?php
////////////////////////////////////////////////////////////
// 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
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment