WooCommerce: Add Column to “Users” Admin Table

On the admin side, you might need to display WooCommerce information inside the users table (WordPress Dashboard > Users). For example, their billing country. Or their phone number. Or maybe some custom calculation e.g. the number of completed orders.

Either way, this is super easy. First, we add a new column – second, we tell what content should go inside it. Enjoy!

Display a custom column @ Users WordPress Dashboard

PHP Snippet 1: Show a Custom WooCommerce Column @ WordPress Admin Users Table (billing country)

/**
 * @snippet       Billing Country @ WordPress Admin Users Table
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */ 

add_filter( 'manage_users_columns', 'bbloomer_add_new_user_column' );

function bbloomer_add_new_user_column( $columns ) {
    $columns['billing_country'] = 'Billing Country';
    return $columns;
}

add_filter( 'manage_users_custom_column', 'bbloomer_add_new_user_column_content', 10, 3 );

function bbloomer_add_new_user_column_content( $content, $column, $user_id ) {

    if ( 'billing_country' === $column ) {
        $customer = new WC_Customer( $user_id );
        $content = $customer->get_billing_country();
    }

    return $content;
}

PHP Snippet 2: Show a Custom WooCommerce Column @ WordPress Admin Users Table (billing phone)

/**
 * @snippet       Billing Phone @ WordPress Admin Users Table
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */ 

add_filter( 'manage_users_columns', 'bbloomer_add_new_user_column' );

function bbloomer_add_new_user_column( $columns ) {
    $columns['billing_phone'] = 'Phone';
    return $columns;
}

add_filter( 'manage_users_custom_column', 'bbloomer_add_new_user_column_content', 10, 3 );

function bbloomer_add_new_user_column_content( $content, $column, $user_id ) {

    if ( 'billing_phone' === $column ) {
        $customer = new WC_Customer( $user_id );
        $content = $customer->get_billing_phone();
    }

    return $content;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

19 thoughts on “WooCommerce: Add Column to “Users” Admin Table

  1. Thank you so far with above snippets!!
    But how can you insert a new column displaying the number of items each user (customer) has?

    1. Thomas, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hello thank you very much, I have been visiting this website for several years, congratulations, it’s amazing, I always spend hours surfing it.

    After several hours playing with ChatGPT, I adapted your snippet to show two columns, amount of orders and total spend.

    It works very well, Rodolfo I would like you to evaluate the snippet, I made sure to implement a caching to improve performance as I have more than 5,000 registered users.

    /**
     * @snippet       Order Count & Total Spend @ WordPress Admin Users Table
     * @description   Adds custom columns to the users table in the WordPress admin area to display the number of orders and the total amount spent by WooCommerce customers, and allows sorting by these columns.
     * @how-to        businessbloomer.com/woocommerce-customization
     * @author        Rodolfo Melogli, Business Bloomer
     * @compatible    WooCommerce 8
     * @community     https://businessbloomer.com/club/
     * @adapted-by    Jorge Abreo with support from ChatGPT
     */
    
    if ( is_admin() ) {
    
        add_action( 'current_screen', 'bbloomer_load_custom_user_columns' );
    
        /**
         * Load custom columns for the users screen
         */
        function bbloomer_load_custom_user_columns() {
            $screen = get_current_screen();
            
            if ( 'users' === $screen->id ) {
    
                // Add custom columns to the users table
                add_filter( 'manage_users_columns', 'bbloomer_add_new_user_columns' );
    
                /**
                 * Add custom columns to the users table
                 * 
                 * @param array $columns The existing columns in the users table
                 * @return array The modified columns with the new custom columns
                 */
                function bbloomer_add_new_user_columns( $columns ) {
                    $columns['order_count'] = 'Orders';
                    $columns['total_spend'] = 'Total Spend';
                    return $columns;
                }
    
                // Populate the custom columns with content
                add_filter( 'manage_users_custom_column', 'bbloomer_add_new_user_columns_content', 10, 3 );
    
                /**
                 * Populate the custom columns with content
                 * 
                 * @param string $content The existing content of the column
                 * @param string $column The column name
                 * @param int $user_id The ID of the user for the current row
                 * @return string The content to be displayed in the column
                 */
                function bbloomer_add_new_user_columns_content( $content, $column, $user_id ) {
                    if ( 'order_count' === $column ) {
                        $order_count = get_user_meta( $user_id, 'order_count', true );
                        if ($order_count === '') {
                            $order_count = 0;
                        }
                        $content = $order_count;
                    } elseif ( 'total_spend' === $column ) {
                        $total_spend = get_user_meta( $user_id, 'total_spend', true );
                        if ($total_spend === '') {
                            $total_spend = wc_get_customer_total_spent( $user_id );
                            update_user_meta( $user_id, 'total_spend', $total_spend );
                        }
                        $content = wc_price( $total_spend );
                    }
                    return $content;
                }
    
                // Make the custom columns sortable
                add_filter( 'manage_users_sortable_columns', 'bbloomer_make_user_columns_sortable' );
    
                /**
                 * Make the custom columns sortable
                 * 
                 * @param array $columns The existing sortable columns
                 * @return array The modified sortable columns with the new custom columns
                 */
                function bbloomer_make_user_columns_sortable( $columns ) {
                    $columns['order_count'] = 'order_count';
                    $columns['total_spend'] = 'total_spend';
                    return $columns;
                }
    
                // Sort users by the custom columns
                add_action( 'pre_get_users', 'bbloomer_order_users_by_custom_columns' );
    
                /**
                 * Sort users by the custom columns
                 * 
                 * @param WP_User_Query $query The WP_User_Query instance
                 */
                function bbloomer_order_users_by_custom_columns( $query ) {
                    if ( is_admin() && isset( $_GET['orderby'] ) ) {
                        if ( 'order_count' == $_GET['orderby'] ) {
                            $query->query_vars['meta_key'] = 'order_count';
                            $query->query_vars['orderby'] = 'meta_value_num';
                        } elseif ( 'total_spend' == $_GET['orderby'] ) {
                            $query->query_vars['meta_key'] = 'total_spend';
                            $query->query_vars['orderby'] = 'meta_value_num';
                        }
                    }
                }
    
                // Save the order count and total spend in user meta to improve performance
                add_action( 'woocommerce_order_status_completed', 'bbloomer_update_user_meta_on_order_complete' );
    
                /**
                 * Update user meta when an order is completed
                 * 
                 * @param int $order_id The ID of the completed order
                 */
                function bbloomer_update_user_meta_on_order_complete( $order_id ) {
                    $order = wc_get_order( $order_id );
                    if ( is_a( $order, 'WC_Order' ) ) {
                        $user_id = $order->get_user_id();
                        if ( $user_id ) {
                            // Check and update the order count
                            $new_order_count = bbloomer_get_customer_order_count( $user_id );
                            $current_order_count = get_user_meta( $user_id, 'order_count', true );
    
                            if ( $new_order_count != $current_order_count ) {
                                update_user_meta( $user_id, 'order_count', $new_order_count );
                            }
    
                            // Check and update the total spend
                            $new_total_spend = wc_get_customer_total_spent( $user_id );
                            $current_total_spend = get_user_meta( $user_id, 'total_spend', true );
    
                            if ( $new_total_spend != $current_total_spend ) {
                                update_user_meta( $user_id, 'total_spend', $new_total_spend );
                            }
                        }
                    }
                }
    
                // Initialize the 'order_count' and 'total_spend' meta for existing users in batches
                function bbloomer_initialize_user_meta_for_existing_users( $batch_size = 100 ) {
                    $paged = 1;
                    do {
                        $users = get_users( array(
                            'fields' => 'ID',
                            'number' => $batch_size,
                            'paged' => $paged
                        ) );
    
                        foreach ( $users as $user_id ) {
                            $order_count = bbloomer_get_customer_order_count( $user_id );
                            update_user_meta( $user_id, 'order_count', $order_count );
    
                            $total_spend = wc_get_customer_total_spent( $user_id );
                            update_user_meta( $user_id, 'total_spend', $total_spend );
                        }
    
                        $paged++;
                    } while ( count( $users ) > 0 );
                }
                add_action( 'admin_init', 'bbloomer_initialize_user_meta_for_existing_users' );
            }
        }
    }
    
    // Custom function to get the order count for a user with caching
    function bbloomer_get_customer_order_count( $user_id ) {
        $transient_key = 'order_count_' . $user_id;
        $order_count = get_transient( $transient_key );
    
        if ( $order_count === false ) {
            $customer_orders = wc_get_orders( array(
                'customer' => $user_id,
                'limit' => -1,
                'status' => 'completed'
            ) );
    
            $order_count = count( $customer_orders );
            set_transient( $transient_key, $order_count, DAY_IN_SECONDS );
        }
    
        return $order_count;
    }
    
    // Custom function to get the total spend for a user with caching
    function bbloomer_get_customer_total_spend( $user_id ) {
        $transient_key = 'total_spend_' . $user_id;
        $total_spend = get_transient( $transient_key );
    
        if ( $total_spend === false ) {
            $total_spend = wc_get_customer_total_spent( $user_id );
            set_transient( $transient_key, $total_spend, DAY_IN_SECONDS );
        }
    
        return $total_spend;
    }
    
  3. I have written simple code to show Order Count for customer:

      $content =wc_get_customer_order_count ($user_id);

    How can I make this column sortable?

  4. Hey,
    great snippet.
    But Iยดm facing a little issue with that. Im trying to insert the column for number of completet orders, but Im not able to find the right column. Billing_country and for example address are working fine. Any chance to tell me what column I need to define?

    Cheers

    1. You can rename “billing_country” to whatever you like in the snippet, the important part is what you will do with the $customer variable. You will need to “read” all customer orders from there

  5. The best woocoommerce page/tips! Thansk a lot!

    Can I add a column indicating that the purchase made was the customer’s first purchase?

    Thanks for helping the community

    1. Hey Jorge, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Hey Rodolfo, this was a super helpful script. Was able to tweak it to show the Last Order date instead and had a question. Is there a way to make the new WC_Customer column sortable? Attempts to sort by the order date sorts by the username instead. Any ideas?

    1. Jen, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Very useful snippet! Hey Rodolfo, could you please tell how to do it for multiple columns? Need to repeat the code or is there any efficient way? TIA

    1. No need to create a new function, just add a new column:

      $columns['billing_country'] = 'Billing Country';
      $columns['new_col'] = 'New Col';
      
      1. Hi there
        If I have to add billing country and billing_city, for example, how can I write the code?
        I have added this

        add_filter( 'manage_users_columns', 'bbloomer_add_new_user_column' );
          
        function bbloomer_add_new_user_column( $columns ) {
            $columns['billing_company'] = 'Ragione Sociale';
        $columns['billing_city'] = 'New Col';
            return $columns;
        }
          
        add_filter( 'manage_users_custom_column', 'bbloomer_add_new_user_column_content', 10, 3 );
          
        function bbloomer_add_new_user_column_content( $content, $column, $user_id ) {
            
            if ( 'billing_company' === $column ) {
           $customer = new WC_Customer( $user_id );
                $content = $customer->get_billing_company();
            }
            
            
            if ( 'billing_city' === $column ) {
           $customer = new WC_Customer( $user_id );
                $content = $customer->get_billing_city();
            }
            return $content;
        }
        

        Is it correct?
        Best regards

        1. The code above works, but if I want to add a custom field (billing_wooccm16 that is a select type field with option of dropdown menu) and I write so

          add_filter( 'manage_users_columns', 'bbloomer_add_new_user_column' );
             
          function bbloomer_add_new_user_column( $columns ) {
              $columns['billing_company'] = 'Ragione Sociale';
          $columns['billing_wooccm16'] = 'New Col';
              return $columns;
          }
             
          add_filter( 'manage_users_custom_column', 'bbloomer_add_new_user_column_content', 10, 3 );
             
          function bbloomer_add_new_user_column_content( $content, $column, $user_id ) {
               
              if ( 'billing_company' === $column ) {
             $customer = new WC_Customer( $user_id );
                  $content = $customer->get_billing_company();
              }
               
               
              if ( 'billing_wooccm16' === $column ) {
             $customer = new WC_Customer( $user_id );
                  $content = $customer->get_billing_wooccm16();
              }
              return $content;
          }
           

          This code doesn’t work

          1. $customer->get_billing…. is used by WooCommerce to get WooCommerce fields, so that won’t work with a custom field. You should study the documentation of the plugin you used to add the custom field, and see how to retrieve it – that will give you the correct code to use. Hope this helps!

  8. Please help me Can add columns the day users create an account ? Thanks.

    1. Hello Kinh, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *