在“ Woocommerce设置”中为客户列表内容添加自定义“设置”选项卡

用户名

即使我不完全了解所有内容,我还是设法将它们放在一起。目的是要有一个名为“客户列表”的自定义选项卡,并在该选项卡(WooCommerce设置->客户列表)中显示所有客户及其详细信息的列表。

它也仅在只有一个用户的情况下起作用。

问题是,我array只获得Order Count,因为Last Order我什至不知道如何获得它。

桌子本身被压在一起,很难阅读。无论如何,非常感谢您提供帮助以使其工作并看起来更好。

这是代码:

add_action( 'woocommerce_settings_tabs', 'add_customer_tab_to_wc' );
function add_customer_tab_to_wc() {
$current_tab = ( $_GET['tab'] == 'customer' ) ? 'nav-tab-active' : '';
echo '<a href="admin.php?page=wc-settings&amp;tab=customer" class="nav-tab '.$current_tab.'">'.__( "Customer List", "astra" ).'</a>'; }

add_action( 'woocommerce_settings_custom', 'customer_list_content' );
function customer_list_content() {

global $wpdb;
$user_id = get_current_user_id();
$customer_total_purchase_sum = $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 LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user'
AND pm2.meta_value LIKE $user_id
" );

$customer = wp_get_current_user();
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key'    => '_customer_user',
'meta_value'  => get_current_user_id(),
'post_type'   => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );

global $user_login;
$user = get_user_by('login', $user_login );
$customeremail = $user->billing_email;

$customers = get_users( 'orderby=nicename&role=customer' );
foreach ( $customers as $user) {
$table_display = '<table class="user-data">
<thead>
<tr>
<th style="font-weight: bold;">'. __( 'First Name', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Lat Name', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Address', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'ZIP Code', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'City', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Phone', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Email', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Money Spent', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Order Count', 'woocommerce' ) .'</th>
<th style="font-weight: bold;">'. __( 'Last Order', 'woocommerce' ) .'</th>
</tr>
</thead>
<tbody>';

$table_display .= '
<tr>
<td>' . esc_html( $user->first_name ) .'</td>
<td>' . esc_html( $user->last_name ) .'</td>
<td>' . get_user_meta( $user->ID, 'billing_address_1', true ) .'</td>
<td>' . get_user_meta( $user->ID, 'billing_postcode', true ) .'</td>
<td>' . get_user_meta( $user->ID, 'billing_city', true ) .'</td>
<td>' . get_user_meta( $user->ID, 'billing_phone', true ) .'</td>
<td>' . $customeremail .'</td>
<td>'. $customer_total_purchase_sum .'</td>
<td>'. $customer_orders .'</td>
<td>last order</td>
</tr>';
$table_display .= '
</tbody>
</table>';
echo $table_display;
}
}
LoicTheAztec

您的代码中有很多错误和错误。我已经重新审视了您的代码,并使其更加轻巧:

// 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 '<a href="admin.php?page=wc-settings&tab=customer_list" class="nav-tab '.$current_tab.'">'.__( "Customer List", "woocommerce" ).'</a>';
}

// 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 '<style> table.user-data th { font-weight: bold; } table.user-data, th, td { border: solid 1px #999; } </style>';

    $table_display = '<table class="user-data" cellspacing="0" cellpadding="6"><thead><tr>
    <th>'. __( 'ID', 'woocommerce' ) .'</th>
    <th>'. __( 'First Name', 'woocommerce' ) .'</th>
    <th>'. __( 'Lat Name', 'woocommerce' ) .'</th>
    <th>'. __( 'Address', 'woocommerce' ) .'</th>
    <th>'. __( 'ZIP Code', 'woocommerce' ) .'</th>
    <th>'. __( 'City', 'woocommerce' ) .'</th>
    <th>'. __( 'Phone', 'woocommerce' ) .'</th>
    <th>'. __( 'Email', 'woocommerce' ) .'</th>
    <th>'. __( 'Total Spent', 'woocommerce' ) .'</th>
    <th>'. __( 'Order count', 'woocommerce' ) .'</th>
    <th>'. __( 'Last order', 'woocommerce' ) .'</th>
    </tr></thead>
    <tbody>';

    // 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  .= '<tr>
        <td align="center"><a href="'.$user_link.'">' . esc_attr( $customer->ID ) .'</a></td>
        <td>' . esc_html( $customer->first_name ) .'</td>
        <td>' . esc_html( $customer->last_name ) .'</td>
        <td>' . esc_html( $customer->billing_address_1 ) .'</td>
        <td>' . esc_attr( $customer->billing_postcode ) .'</td>
        <td>' . esc_attr( $customer->billing_city ) .'</td>
        <td>' . esc_attr( $customer->billing_phone ) .'</td>
        <td><a href="mailto:'.$customer->billing_email.'">' . esc_attr( $customer->billing_email ) .'</a></td>
        <td align="right">'. ( $total_purchased > 0 ? wc_price( $total_purchased ) : ' - ' ) . '</td>
        <td align="center">'. $orders_count . '</td>
        <td align="center"><a href="'.$last_order_link.'">' .  ( $last_order_id > 0 ? $last_order_id : ' - ' ) . '</a></td>
        </tr>';
    }
    // Output the table
    echo $table_display . '</tbody></table>';
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

注意:当你将拥有更多的客户,你的方法将是太重(过多查询和数据显示)......所以,你应该需要做一个WP_User_Query包括分页多个子页面拆分数据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Woocommerce 中为简单产品添加自定义设置选项卡

在WooCommerce中以编程方式添加自定义设置选项卡以管理产品数据

为选项卡布局中的每个选项卡设置自定义视图时出错

将自定义字段添加到产品设置运送选项卡,并在WooCommerce的其他信息选项卡上显示值

无法将高度设置为自定义选项卡布局

在Woocommerce Admin产品页面中编辑“自定义产品”选项卡内容

Woocommerce的单个产品附加选项卡中显示的自定义metabox内容

如何在 Woocommerce 的自定义产品类型中添加变体选项卡?

在功能区中添加自定义选项卡

设置为使用自定义设计隐藏时,复选框选项卡索引不起作用

当woocommerce产品中不存在任何内容时,隐藏自定义选项卡

iOS 8:在界面构建器中为自定义选项卡栏项设置的所选图像不起作用

如何在附加信息选项卡 woocommerce 中向自定义字段添加默认值

在WooCommerce中将“相关产品”添加到自定义选项卡

将自定义字段添加到“其他信息”选项卡(WooCommerce)

显示在附加信息选项卡中,一些产品设置自定义字段值

具有自定义内容的Vaadin选项卡

Angular 自定义指令在 Enter 键按下时设置选项卡索引

如何使用自定义类设置选项卡的样式?

我想在React Native的选项卡导航的底部选项卡中添加自定义图标

在 WooCommerce 单一产品自定义选项卡中显示交叉销售

Woocommerce单一产品选项卡中的自定义字段

将产品变体的自定义字段值显示到WooCommerce中的“自定义产品”选项卡

如何在包含内容的新选项卡中打开自定义 URL?

SAP B1 中的自定义选项卡内容未更改

是否可以在“详细信息”选项卡中添加自定义字段

在 Microsoft 团队的自定义应用程序中添加选项卡

如何在UITabBarController中的选项卡上添加自定义手势

如何调用“自定义”选项卡中的“添加任务”操作以显示销售订单的任务?