Search code examples
wordpresswoocommercebackendhook-woocommerceorders

Add custom column with metadata on WooCommerce admin orders list


I'm want to add custom column(s) with some meta data on the WooCommerce admin orders list. I found, and modified this for my needs, and put it in my functions.php:

add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );
    
    //edit this for your column(s)
    //all of your columns will be added before the actions column
    $new_columns['dname'] = 'Dogs Name';
    $new_columns['additional_allergies'] = 'Allergies';
    
    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

At checkout I collect these two meta keys:

  • dname
  • additional_allergies

However, my current code only shows empty columns, any advice to add the meta datea?


Solution

  • The manage_shop_order_posts_custom_column hook can be used to add a header.

    // Add a Header
    function custom_shop_order_column( $columns ) {
        // Add new columns
        $columns['dogs_name'] = __( 'Dogs Name', 'woocommerce' );
        $columns['additional_allergies'] = __( 'Allergies', 'woocommerce' );
        
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );
    

    The manage_shop_order_posts_custom_column hook will be needed to populate the column.

    • Note: it is important to determine whether it is metadata that belongs to the $order object or whether it is metadata that belongs to the order $items and based on that you will have to use 1 of the 2 answers below.

    1a: In case the metadata belongs to the $order object, you can use:

    //  Populate the Column
    function custom_shop_order_list_column_content( $column, $post_id ) {
        // Get order object
        $order = wc_get_order( $post_id );
    
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Compare column name
            if ( $column == 'dogs_name' ) {
                // Get meta, use the correct meta key!
                $dogs_name = $order->get_meta( 'dname' );
                
                // NOT empty
                if ( ! empty( $dogs_name ) ) {
                    // Output
                    echo $dogs_name;
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
                }
            }
    
            // Compare column name
            if ( $column == 'additional_allergies' ) {
                // Get meta, use the correct meta key!
                $allergies = $order->get_meta( 'additional_allergies' );
                
                // NOT empty
                if ( ! empty( $allergies ) ) {
                    // Output
                    echo $allergies;
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
                }       
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );
    

    1b: However, when the metadata belong to the order $items then an adjustment will be necessary in the above answer, since an $order can consist of several $items, we will loop through the $order object with foreach

    //  Populate the Column
    function custom_shop_order_list_column_content( $column, $post_id ) {
        // Get order object
        $order = wc_get_order( $post_id );
    
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Initialize
            $dogs_name_arr = array();
            $allergies_arr = array();
            
            // Loop trough order items
            foreach ( $order->get_items() as $item_key => $item ) {
                // Get meta, use the correct meta key!
                $dogs_name = $item->get_meta( 'dname' );
                
                // NOT empty
                if ( ! empty ( $dogs_name ) ) {
                    // Push to array
                    $dogs_name_arr[] = $dogs_name;
                }
                
                // Get meta, use the correct meta key!
                $allergies = $item->get_meta( 'additional_allergies' );
                
                // NOT empty
                if ( ! empty ( $allergies ) ) {
                    // Push to array
                    $allergies_arr[] = $allergies;
                }
            }
            
            // Compare column name
            if ( $column == 'dogs_name' ) {
                // NOT empty
                if ( ! empty ( $dogs_name_arr ) ) {
                    // Output
                    echo '<ul>';
                    echo '<li>' . implode( '</li><li>', $dogs_name_arr ) . '</li>';
                    echo '</ul>';
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
                }
            }
    
            // Compare column name
            if ( $column == 'additional_allergies' ) {
                // NOT empty
                if ( ! empty ( $allergies_arr ) ) {
                    // Output
                    echo '<ul>';
                    echo '<li>' . implode( '</li><li>', $allergies_arr ) . '</li>';
                    echo '</ul>';
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
                }
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );