Add multiple custom columns to WooCommerce "My account" orders table
I am trying to add a few columns to the WooCommerce > my account > orders page.
So far the code below adds more than one column but the content of the columns are not showing, where I'm I getting it wrong?
add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
unset($columns['order-total']);
$columns['custom-column'] = __( 'New Column', 'woocommerce' );
$columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );
return $columns;
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
// Example with a custom field
if( $columns == 'custom-column' ) {
echo 'Hello';
}
if( $columns == 'custom-column2' ) {
echo 'Hello 2';
}
}
The woocommerce_account_orders_columns
filter hook allows us to add 1 or more columns
// Add new column(s) to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
$columns['custom-column'] = __( 'New Column 1', 'woocommerce' );
$columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );
return $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
However, adding content per column is done via the woocommerce_my_account_my_orders_column_{$column_id}
action hook.
So {$column_id}
need to be replaced by the column key slug (custom-column
or custom-column2
) in this particular case.
It is therefore not necessary to determine the correct column in the callback function via an if condition
So you get:
// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column( $order ) {
echo 'New Column 1';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'filter_woocommerce_my_account_my_orders_column_custom_column', 10, 1 );
// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column2( $order ) {
echo 'New Column 2';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column2', 'filter_woocommerce_my_account_my_orders_column_custom_column2', 10, 1 );