Display customer cancelled orders count in Woocommerce Admin Edit Orders pages Display customer cancelled orders count in Woocommerce Admin Edit Orders pages wordpress wordpress

Display customer cancelled orders count in Woocommerce Admin Edit Orders pages


You need to target the Customer ID from the current order. It can be done in a much simpler way.

You should try this:

add_action( 'woocommerce_admin_order_data_after_order_details', 'get_specific_customer_orders', 10, 1 );function get_specific_customer_orders( $order ) {    $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'),    ) );    $orders_count = '<strong style="color:#ca4a1f">' . count($customer_orders) . '</strong>';    echo'<br clear="all">    <p>' . __( 'Cancelled orders count: ' ) . $orders_count . '</p>';}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works.


I used this for my project:
use

get_current_user_id()

instead of

$order->get_customer_id()

add_shortcode( 'geo-get-customer-orders', 'geo_get_customer_orders' );function geo_get_customer_orders() {    global $order;    $customer_orders = get_posts( array(        'numberposts' => -1,        'meta_key'    => '_customer_user',        'meta_value'  => get_current_user_id(),        'post_type'   => 'shop_order',        'post_status' => array('wc-completed'),    ) );    echo count($customer_orders);}

finally use this shortcode:

[geo-get-customer-orders]