I want to have a custom column in my WooCommerce admin orders page.
it’s about WHO CREATED AN ORDER ( actually I want the author name ). With HPOS in WooCommerce I cant Use the normall codes. beacuse tables of woocommerce has chagned.
function add_wc_order_list_custom_column( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_status' === $column_name ) {
$new_columns['custom_order_list_action'] = 'Custom Column';
}
}
return $new_columns;
}
function display_wc_order_list_custom_column_content( $column, $order ) {
if ( $column == 'custom_order_list_action' ) {
$author_id = $order->get_customer_id();
$author = get_userdata( $author_id );
echo $author ? $author->display_name : 'Unknown Author';
}
}
Thanks for your help . . .
I’ve tried so many ways from Ai,forums and etc and I want from Programmers to help me with this problem.
3
There are mainly 3 cases for WooCommerce order creation (related to the order author):
1). Orders placed via checkout:
When an order is placed via checkout, it’s always created by the customer (billing first name and last name) for both Guests and registered users.
You already get this information via the first column from admin orders list.
And you can see on “Origin” column “Direct”.
Note that when HPOS is not enabled or enabled with compatibility mode, the “post_author” is set to 0
.
2). Orders created manually via the admin:
In this case, the author is the authorized user (administrator or shop manager) that creates the order.
From admin orders list, on “Origin” column, you will see “Web admin”.
If HPOS is enabled without compatibility mode, the author information is only partially available via the order admin notes, where you can get somehow the author display name and email (but it’s not reliable).
If HPOS is enabled with compatibility mode, you can get the author ID from the WC_Order
object like:
$author_id = get_post_field( 'post_author', $order->get_id() );
3). Orders created Programmatically:
Here, the origin and author information is only available if it’s set via the code.
You can use the WC_Order
property “created_via” using get_created_via()
method to see how the order has been created. The known displayed values are:
- “checkout” for orders placed via checkout (1),
- “admin” for orders created manually via the backend (2).
When creating an order programmatically (3), you can define “created_via” using set_created_via()
method.
For orders created manually when HPOS is enabled (without compatibility mode), you can save the author ID using the following:
add_action( 'woocommerce_process_shop_order_meta', 'set_author_id_on_manual_orders', 40 );
function set_author_id_on_manual_orders( $order_id ) {
global $current_user;
$order = wc_get_order($order_id); // Get WC_Order object instance
//check if author ID exist
if ( ! $order->get_meta('author_id') && ! $order->is_created_via('checkout') ) {
$order->update_meta_data( 'author_id', $current_user->ID );
$order->save();
}
}
Code goes in functions.php file of your child theme (or in a plugin).
Then to get the author ID use $author_id = $order->get_meta('author_id');
3