bjornen
bjornen

Reputation: 107

Display in Woocommerce Orders Admin list users failed and cancelled orders count

I've added the column to the order table (is it called that) and I'm now trying to first count how many, if any, failed and cancelled orders the user has and then display that amount using echo. Here is my code, which shows the column but displays a blank column.

Any help is highly appreciated.

The code:

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

Upvotes: 1

Views: 440

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253773

The code below use a very simple and much lighter SQL query to get the count of user orders with a cancelled and failed status. Also there is some mistakes in your code.

Your revisited code:

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 1

Related Questions