添加自定义设置选项卡,Woocommerce的设置客户列表内容

问题描述 投票:2回答:1

我已经成功地把这个在一起,即使我不完全了解这一切。我们的目标是有一个名为“客户名单”,并在该选项卡自定义选项卡(WooCommerce设置 - >客户列表)显示所有客户及其细节的列表。

这如果只有一个用户也只适用。

问题是,我得到array只为Order CountLast 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;
}
}
php wordpress woocommerce settings user-data
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.