在Woocommerce Orders Admin列表中显示用户失败和取消的订单数

时间:2018-09-06 15:38:32

标签: php wordpress woocommerce backend orders

我已将该列添加到订单表中(即所谓的),我现在尝试首先计算用户有多少(如果有)失败和取消的订单,然后使用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;
    }
}

1 个答案:

答案 0 :(得分: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文件中。经过测试,可以正常工作。