0

I'm looking to add a column to the Woocommerce Orders page - specifically "company name". This is listed in the orders as 'Customer'. I'd like to display this as a column in the orders page.

Paul M
  • 115
  • 12

2 Answers2

1
<pre>
Step 1:-
/* add custom column under order listing page */
/**
 * Add 'Company name' column header to 'Orders' page immediately after 'Total' column.
 *
 * @param string[] $columns
 * @return string[] $new_columns
 */
function sv_wc_cogs_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['company_name'] = __( 'Company name', 'my-textdomain' );
        }
    }

    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'sv_wc_cogs_add_order_profit_column_header', 20 );

Step 2:-

/**
 * Add 'Company name' column content to 'Orders' page immediately after 'Total' column.
 *
 * @param string[] $column name of column being displayed
 */
function sv_wc_cogs_add_order_profit_column_content( $column ) {
    global $post;

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

       
        $company_name = !empty(get_post_meta($post->ID,'company_name',true)) ? get_post_meta($post->ID,'company_name',true) : 'N/A';
        

        echo $company_name;
    }
    
}
add_action( 'manage_shop_order_posts_custom_column', 'sv_wc_cogs_add_order_profit_column_content' );
</pre>

Copy above code in theme functions.php file

  • thankyou, i'll try this. you're a star. – Paul M Jul 22 '22 at 13:12
  • so i tried this, and the company name column appears - which is great thankyou. it's showing N/A in the column though for each order - needs to be the 'company' field from the customer billing address. i'll update this in the question. – Paul M Jul 22 '22 at 13:32
  • Are you taking about company name here which appears in billing details at checkout page? – Rahul Gupta Jul 22 '22 at 13:41
  • Yes - just tried replacing company_name with billing_company but still get the N/A. Can't seem to find a page with order meta values. – Paul M Jul 22 '22 at 13:56
1
Change code from 

 $company_name = !empty(get_post_meta($post->ID,'company_name',true)) ? get_post_meta($post->ID,'company_name',true) : 'N/A';

To 

 $company_name = !empty(get_post_meta($post->ID,'_billing_company',true)) ? get_post_meta($post->ID,'_billing_company',true) : 'N/A';

I hope will this work.