【问题标题】:Add a custom Settings tab to Woocommerce Settings for Customer list content将自定义设置选项卡添加到客户列表内容的 Woocommerce 设置
【发布时间】:2019-06-27 08:46:39
【问题描述】:

尽管我并不完全理解这一切,但我还是设法把它放在一起。目标是创建一个名为“客户列表”的自定义选项卡,并在该选项卡(WooCommerce 设置 -> 客户列表)中显示所有客户及其详细信息的列表。

它也只适用于只有一个用户。

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

表格本身有点压在一起,难以阅读。无论如何,非常感谢任何关于让它工作和看起来不错的帮助。

代码如下:

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;
}
}

【问题讨论】:

  • 请不要破坏您的帖子。 Stack Overflow 上不允许破坏行为,并且将被恢复。此外,反复破坏帖子可能会导致网站暂停。

标签: php wordpress woocommerce settings user-data


【解决方案1】:

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

// 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 包括 pagination 来将数据拆分为多个子页面。

【讨论】:

  • 再次感谢您!我只需要弄清楚如何将整个内容放入订单视图(可排序的列)。
  • 我现在自己尝试添加分页后提出了一个新问题。如果可能的话,请看看。 stackoverflow.com/questions/54525539/…
猜你喜欢
  • 1970-01-01
  • 2018-09-22
  • 2017-12-26
  • 2020-12-21
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多