Created
April 20, 2024 01:15
-
-
Save razorfrog/7b33abfd390771d2c2873e2b27a5374c 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
<?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