phpwoocommerceadminordershighperformance

Add customer order count to WooCommerce admin orders list (HPOS)


Based on Add columns to admin orders list in WooCommerce (+ HPOS) answer code, I am adding a custom column in the wc-orders dashboard. but I am unable to get it to work.

The columns display on my wc-orders dashboard, but it's not fetching the metadata of the order count. It is just displaying no value.

Here is the code I have inputted in the functions.php of my child theme

add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_wc_order_list_custom_column' );
function add_wc_order_list_custom_column( $columns ) {
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;

        if( $key ===  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['my-column1'] = __( 'Title1','customer_order_count');
            $reordered_columns['my-column2'] = __( 'Title2','theme_domain');
        }
    }
    return $reordered_columns;
}

add_action('manage_woocommerce_page_wc-orders_custom_column', 'display_wc_order_list_custom_column_content', 10, 2);
function display_wc_order_list_custom_column_content( $column, $order ){
    switch ( $column )
    {
        case 'my-column1' :
            // Get custom order metadata
            $value = $order->get_meta('$orders_count');
            if ( ! empty($value) ) {
                echo $value;
            }
        

        case 'my-column2' :
            // Get custom order metadata
            $value = $order->get_meta('_the_meta_key2');
            if ( ! empty($value) ) {
                echo $value;
            }
            // For testing (to be removed) - Empty value case
            else {
                echo '<small>(<em>no value</em>)</small>';
            }
            break;
    }
}

Solution

  • You are using $orders_count as meta_key, so you should try to remove the character $ at the beginning.

    Now as the Customer order count is set as user metadata, you should use something different like:

    add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_wc_order_list_custom_column' );
    function add_wc_order_list_custom_column( $columns ) {
        $reordered_columns = array();
    
        // Inserting columns to a specific location
        foreach( $columns as $key => $column){
            $reordered_columns[$key] = $column;
    
            if( $key ===  'order_status' ){
                $reordered_columns['order-count'] = __( 'Order count','woocommerce');
            }
        }
        return $reordered_columns;
    }
    
    add_action('manage_woocommerce_page_wc-orders_custom_column', 'display_wc_order_list_custom_column_content', 10, 2);
    function display_wc_order_list_custom_column_content( $column, $order ){
        if ( $column  === 'order-count' ) {
            echo wc_get_customer_order_count( $order->get_user_id() ); // Display customer orders count
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    enter image description here

    Related: Add columns to admin orders list in WooCommerce (+ HPOS)