X
    Categories: WooCommerce Tutorials, Tips and GuidesWordPress Tutorials, Tips and Guides

Add a WooCommerce Orders List Column

Every WooCommerce Orders list has columns that highlight various details about the order. While these columns could be easily remove through the Screen Options tab, the addition of the columns requires custom code that adds a column to the list.

Add a Header

The addition of a custom columns is done through manage_edit-{$post_type}_columns filter (part of the WordPress core). By using this filter, I could change the columns in the list table  and inject my own column(s).

function wc_new_order_column( $columns ) {
$columns['my_column'] = 'My column';
return $columns;
add_filter( 'manage_edit-shop_order_columns', 'wc_new_order_column' );

Now I am going to add a column in my intended position.  This would be a new Profit column that would come after the total column for the order.

function cw_add_order_profit_column_header($columns)
{
    $new_columns = array();
    foreach ($columns as $column_name => $column_info) {
        $new_columns[$column_name] = $column_info;
        if ('order_total' === $column_name) {
            $new_columns['order_profit'] = __('Profit', 'my-textdomain');
        }
    }
    return $new_columns;
}

add_filter('manage_edit-shop_order_columns', 'cw_add_order_profit_column_header');

Now we can use that to get the profit for the order and add it to the column.

function cw_add_order_profit_column_content( $column ) {
    global $post;

    if ( 'order_profit' === $column ) {

        $order    = wc_get_order( $post->ID );
        $currency = is_callable( array( $order, 'get_currency' ) ) ? $order->get_currency() : $order->order_currency;
        $profit   = '';
        $cost     = sv_helper_get_order_meta( $order, '_wc_cog_order_total_cost' );
        $total    = (float) $order->get_total();

               if ( '' !== $cost || false !== $cost ) {

            $cost   = (float) $cost;
            $profit = $total - $cost;
        }

        echo wc_price( $profit, array( 'currency' => $currency ) );
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'cw_add_order_profit_column_content' );

Now, the following is optional. I will make the Profit column the same width as the total column. I will add custom CSS to the existing stylesheet by using the function wp_add_inline_style. I will add this to woocommerce_admin_styles:

function cw_add_order_profit_column_style() {

    $css = '.widefat .column-order_date, .widefat .column-order_profit { width: 9%; }';
    wp_add_inline_style( 'woocommerce_admin_styles', $css );
}
add_action( 'admin_print_styles', 'cw_add_order_profit_column_style' );

Adding custom columns to WooCommerce order list is a matter of adding code through a filter. If you need help with the code and the process of adding the custom columns, do leave a comment below and I will get back to you ASAP.

Owais Alam: is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com