Home > Mobile >  Add a column to the Woocommerce Orders page
Add a column to the Woocommerce Orders page

Time:07-22

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.

CodePudding user response:

<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

CodePudding user response:

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.

  • Related