php页面增加自选项,php - 添加自定义设置选项卡,Woocommerce的设置客户列表内容 - SO中文参考 - www.soinside.com...

有很多代码中的错误和失误。我深深重新审视你的代码,使它更亮:

// 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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值