Search code examples
phpwordpresswoocommercebackendorders

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


I am using WooCommerce plugin for one of my ecommerce WordPress websites. I want to add some columns to my order listing page in the WooCommerce admin area. I am not able to find out where to add that.

Can anyone advise which template page I need to amend in order to meet my requirement?


Solution

  • Updates
    2024: Added code for High-Performance Order Storage HPOS - see at the end.
    2023: Switched to CRUD methods instead of old WordPress post meta functions
    2018: added positioning feature to the new columns

    ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST

    In the example below, we add 2 new custom columns, before existing "Total" and "Actions" columns (doesn't work when HPOS is enabled).

    // ADDING 2 NEW COLUMNS WITH THEIR TITLES (before "Total" and "Actions" columns)
    add_filter( 'manage_edit-shop_order_columns', 'add_admin_order_list_custom_column', 20 );
    function add_admin_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','theme_domain');
                $reordered_columns['my-column2'] = __( 'Title2','theme_domain');
            }
        }
        return $reordered_columns;
    }
    
    // Adding custom fields meta data for each new column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'display_admin_order_list_custom_column_content', 20, 2 );
    function display_admin_order_list_custom_column_content( $column, $post_id )
    {
        global $the_order;
    
        switch ( $column )
        {
            case 'my-column1' :
                // Get custom order metadata
                $value = $the_order->get_meta('_the_meta_key1');
                if ( ! empty($value) ) {
                    echo $value;
                }
                // For testing (to be removed) - Empty value case
                else {
                    echo '<small>(<em>no value</em>)</small>';
                }
                break;
    
            case 'my-column2' :
                // Get custom order metadata
                $value = $the_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;
        }
    }
    

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

    enter image description here


    FOR High-Performance Order Storage (HPOS):

    From WooCommerce 8.2, released on October 2023, High-Performance Order Storage (HPOS) is officially flagged as stable and will be enabled by default for new installations.

    When HPOS is enabled, to Add columns to admin orders list in WooCommerce requires to use different hooks.

    Below, we add 2 new custom columns, before existing "Total" and "Actions" columns:

    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','theme_domain');
                $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('_the_meta_key1');
                if ( ! empty($value) ) {
                    echo $value;
                }
                // For testing (to be removed) - Empty value case
                else {
                    echo '<small>(<em>no value</em>)</small>';
                }
                break;
    
            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;
        }
    }
    

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


    Related answer (for products): Add custom columns to admin products list in WooCommerce 3