有很多代码中的错误和失误。我深深重新审视你的代码,使它更亮:
// Add a custom setting tab to Woocommerce > Settings section
add_action( 'woocommerce_settings_tabs', 'wc_settings_tabs_customer_list_tab' );
function wc_settings_tabs_customer_list_tab() {
$current_tab = ( isset($_GET['tab']) && $_GET['tab'] === 'customer_list' ) ? 'nav-tab-active' : '';
echo ''.__( "Customer List", "woocommerce" ).'';
}
// The setting tab content
add_action( 'woocommerce_settings_customer_list', 'display_customer_list_tab_content' );
function display_customer_list_tab_content() {
global $wpdb;
// Styling the table a bit
echo '';
$table_display = '
// Loop through customers
foreach ( get_users( 'orderby=nicename&role=customer' ) as $key => $customer ) {
// Customer total purchased
$total_purchased = (float) $wpdb->get_var( "
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
WHERE p.post_status = 'wc-completed' AND p.post_type = 'shop_order'
AND pm.meta_key = '_order_total' AND pm2.meta_key = '_customer_user'
AND pm2.meta_value = {$customer->ID}
" );
// Customer orders count
$orders_count = (int) $wpdb->get_var( "
SELECT DISTINCT COUNT(p.ID) FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_order' AND pm.meta_key = '_customer_user'
AND pm.meta_value = {$customer->ID}
" );
// Customer last order ID
$last_order_id = (int) $wpdb->get_var( "
SELECT MAX(p.ID) FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_order' AND pm.meta_key = '_customer_user'
AND pm.meta_value = {$customer->ID}
" );
$user_link = 'user-edit.php?user_id=' . $customer->ID;
$last_order_link = 'post.php?post='.$last_order_id.'&action=edit';
$table_display .= '
' . esc_attr( $customer->ID ) .'' . esc_html( $customer->first_name ) .'' . esc_html( $customer->last_name ) .'' . esc_html( $customer->billing_address_1 ) .'' . esc_attr( $customer->billing_postcode ) .'' . esc_attr( $customer->billing_city ) .'' . esc_attr( $customer->billing_phone ) .' ' . esc_attr( $customer->billing_email ) .''. ( $total_purchased > 0 ? wc_price( $total_purchased ) : ' - ' ) . ''. $orders_count . ' ' . ( $last_order_id > 0 ? $last_order_id : ' - ' ) . '';}
// Output the table
echo $table_display . '
';}
代码放在您的活动子主题的function.php文件(或活动主题)。测试和工程。
注意:当你将拥有更多的客户,你的方法将是太重(太多的查询和数据显示)......所以,你应该需要做,包括WP_User_Query在多个子页面拆分数据pagination。