在Woocommerce订单中显示管理员列表用户失败并取消订单计数

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

我已将列添加到订单表(它被称为),我现在尝试首先计算用户有多少(如果有的话)失败和取消订单,然后使用echo显示该数量。这是我的代码,它显示了列但显示了一个空白列。

任何帮助都非常感谢。

代码:

function add_failed_orders_column_to_order_page( $columns ) {

$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;

if ( 'order_total' === $column_name ) {
$new_columns['previous_failed_customer_orders'] = __( 'Failed Orders', 'ocean-child' );
} }
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_failed_orders_column_to_order_page', 20 );



add_action( 'manage_shop_order_posts_custom_column' , 'display_previous_failed_orders', 10, 2 );
function display_previous_failed_orders( $order, $column )
{
    global $order;
    switch ( $column )
    {
        case 'previous_failed_customer_orders' :
         $failed_customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $order->get_customer_id(),
        'post_type'   => 'shop_order',
        'post_status' => array('wc-cancelled', 'wc-failed'),
    ) );
$failed_orders_count = '<strong style="color:red !important; font-size:15px !important;">' . count($failed_customer_orders) . '</strong>'; {
                echo $failed_orders_count;
            }
            break;
    }
}
php wordpress woocommerce backend orders
1个回答
1
投票

下面的代码使用一个非常简单和轻松的SQL查询来获取具有已取消和失败状态的用户订单的计数。您的代码中也存在一些错误。

您重新访问的代码:

add_filter( 'manage_edit-shop_order_columns', 'add_column_user_failled_cancelled', 20, 1 );
function add_column_user_failled_cancelled( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_key => $column_label ) {

        $new_columns[$column_key] = $column_label;

        if ( 'order_total' === $column_key ) {
            $new_columns['cancelled_failled'] = __( 'Failed Orders', 'ocean-child' );
        }
    }
    return $new_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'column_content_user_failled_cancelled', 10, 1 );
function column_content_user_failled_cancelled( $column ) {
    global $post, $the_order, $wpdb;

    if ( $column =='cancelled_failled' ) {

        // Get the count of the user failled and cancelled orders
        $count = $wpdb->get_var( "
            SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
            JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
            WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-cancelled','wc-failed')
            AND pm.meta_key = '_customer_user' AND pm.meta_value = '{$the_order->get_customer_id()}'
        ");

        echo '<strong style="color:red !important; font-size:15px !important;">' . $count . '</strong>';
    }
}

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

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