This code snippet allows you to show an additional, custom column to the table showing all of the orders located under WordPress Dashboard > WooCommerce > Orders.
In this example, I show the Payment Gateway fee.
Why? I think it’s absolutely nuts that in the WooCommerce reports, you only see sales turnover and not how much actually lands in your bank.
There’s no way to see profit because the reports don’t take into account the payment gateway fees and after all, revenue is vanity, profit is sanity!
When I launched my WooCommerce Conversion Rate Optimisation (CRO) eBook, I wanted to see how much each payment gateway took.
At the time of launch, I used Stripe and PayPal, the two most common payment gateways.
I knew the fee amount was stored in the wp_postmeta database table (pre HPOS schema) because when drilling down and viewing an order, I could see the fee as a deduction in the totals.
/**
* Snippet Name: WooCommerce Add Custom Column To Orders Table
* Snippet Author: ecommercehints.com
*/
// Create and show the new orders table column
add_filter( 'manage_edit-shop_order_columns', 'ecommercehints_show_custom_orders_table_column' );
function ecommercehints_show_custom_orders_table_column( $columns ) {
$columns['payment_gateway_fee'] = 'Payment Gateway Fee'; // The ID and Label shown in the table column header
return $columns;
}
/**
* Populate the orders table custom column
* I have chosen to show the payment gateway fee, either from Stripe or PayPal, both stored in the postmeta table (pre HPOS schema)
*/
add_action( 'manage_shop_order_posts_custom_column', 'ecommercehints_populate_orders_custom_table_column' );
function ecommercehints_populate_orders_custom_table_column( $column ) {
global $post;
if ( 'payment_gateway_fee' === $column ) {
$order = wc_get_order( $post->ID );
if ($order->get_meta('PayPal Transaction Fee')) { // If the order was paid with PayPal...
echo wc_price($order->get_meta('PayPal Transaction Fee')); // ...Then show the PayPal fee (it's meta key in the postmeta database table is "PayPal Transaction Fee")
} elseif ($order->get_meta('_stripe_fee')) { // Or, if the order was paid with Stripe...
echo wc_price($order->get_meta('_stripe_fee')); // ...Then show the Stripe fee (it's meta key in the database table is "_stripe_fee")
}
}
}