To add a custom admin column to the WooCommerce orders in the WordPress admin area, you can use the manage_edit-shop_order_columns
and manage_shop_order_posts_custom_column
filters. This allows you to define new columns and populate them with custom data. Here’s an example of how you can achieve this:
function wpsnippets_add_custom_order_columns( $columns ) {
// Add a new column with a custom heading
$columns['custom_column'] = 'Custom Column';
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wpsnippets_add_custom_order_columns' );
function wpsnippets_populate_custom_order_column( $column ) {
global $post;
if ( 'custom_column' === $column ) {
// Get custom data for the order
$custom_data = get_post_meta( $post->ID, 'custom_data', true );
// Output the custom data in the column
echo $custom_data;
}
}
add_action( 'manage_shop_order_posts_custom_column', 'wpsnippets_populate_custom_order_column' );
In this example, we’re adding a custom column called “Custom Column” to the WooCommerce orders screen.
Inside the wpsnippets_add_custom_order_columns
function, the manage_edit-shop_order_columns
filter is used to modify the columns array. We add a new column with the key custom_column
and the desired column heading.
Inside the wpsnippets_populate_custom_order_column
function, the manage_shop_order_posts_custom_column
action is used to populate the custom column with data. We check if the current column being processed is our custom column ('custom_column'
). Then, we retrieve the custom data associated with the order (in this case, custom_data
) using get_post_meta()
and output it in the column.
By adding this code snippet to your WordPress site, you will see a new custom column in the WooCommerce orders screen in the admin area, populated with the desired custom data for each order.
Unfortunately this doesn’t work. I keep getting a “syntax error, unexpected” from my code snippets app, and trying it in functions.php results in failure and “exceptions thrown without a stack frame” warning. Any ideas how to solve it? Many thanks