The Orders listing table in WooCommerce available under WooCommerce > Orders is a basic one and has only a few fields. Here is how it looks by default:
Now you may wonder that this much information might be too less and it would be great if some more information was available on this page.
WooCommerce by default does have the provision to show more fields but they are disabled. The other fields that can be available on this page are:
- Billing
- Ship to, and
- Actions
How to enable these missing fields?
On the Orders screen, there is a button called “Screen Options”. Something like this:
When you click this option you will be able to see the missing fields that I listed above:
Select the fields which you want to enable and click on Apply. Once done, the columns will be displayed in the table.
The Actions column is very much useful for store owners in marking the orders completed and it is recommended that they enable it from here.
How to add custom columns to WooCommerce Order Table?
Now that you enabled a few columns available by default, let’s add a few more columns that you might want to have it on the order listings page.
The example listed below can be used to add any columns related to WooCommerce orders. We will be using the payment gateways as the column to be displayed. The payment gateways could be any payment gateway or a custom payment gateway created using Custom Payment Gateways for WooCommerce.
You can add the below mentioned code in the functions.php file of your child theme or use a plugin like the Code Snippets plugin.
Add a column header
You can add a column using the manage_edit-{$post_type}_columns
filter that is available in WordPress. Since Orders (shop_order) are custom post types as well, our resulting filter would be manage_edit-shop_order_columns
. Here is what the code would look like:
add_filter( 'manage_edit-shop_order_columns', 'is_add_new_order_admin_list_column' );
/**
* Add column key and label.
*
* @param array $columns Columns.
* @return array
*/
function is_add_new_order_admin_list_column( $columns ) {
$columns['payment_gateway'] = 'Payment Gateway';
return $columns;
}
This would add a new column with a header as Payment Gateway as shown below:
As you can see above that the column has been added but there is no data present in those columns.
Populating the column with data
Like manage_edit-{$post_type}_columns
we have another default hook from WordPress that will help us with populating the data. The hook is manage_{$post_type}_posts_custom_column
Here is how to apply the code and get the payment gateways:
add_action( 'manage_shop_order_posts_custom_column', 'is_payment_gateway_column_content' );
/**
* Add Payment Gateways used in the column data.
*
* @param string $column Column Key.
* @return void
*/
function is_payment_gateway_column_content( $column ) {
global $post;
if ( 'payment_gateway' === $column ) {
$order = wc_get_order( $post->ID );
echo esc_html( $order->get_payment_method_title() );
}
}
To get the payment gateway we will need the WC_Order object and that can be created using wc_get_order
function. The Order ID can be obtained from the global $post
object.
Once an Order object is created, the payment gateway title can be obtained using the get_payment_method_title
function.
The payment gateways would now be visible in the columns:
As you can see above, the Custom Payment Gateways created too are visible in the columns.
Using the above method, you can set some other columns as well based on your requirements such as the Shipping method of the order or the items present in the order, or any other custom information stored within the order.
Do let us know in the comments below how it goes with the above code at your end or any difficulties faced while adding the code snippet.