Created
May 26, 2017 10:33
-
-
Save mohammadmursaleen/53235f6d32bb1dfe059557b2007605d7 to your computer and use it in GitHub Desktop.
WooCommerce - Add purchased by column in order list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'manage_shop_order_posts_columns', 'show_purchased_by_shop_order_columns' , 100 ); | |
add_action( 'manage_shop_order_posts_custom_column', 'show_purchased_by_render_shop_order_columns', 2 ); | |
add_filter( 'manage_edit-shop_order_sortable_columns', 'show_purchased_by_shop_order_sortable_columns' ); | |
/** | |
* Define custom columns for orders. | |
* @param array $columns | |
* @return array | |
*/ | |
function show_purchased_by_shop_order_columns( $columns ) { | |
$columns['purchased_by'] = 'Purchased By'; | |
return $columns; | |
} | |
/** | |
* Make columns sortable | |
* @param array $columns | |
* @return array | |
*/ | |
function show_purchased_by_shop_order_sortable_columns( $columns ) { | |
$columns['purchased_by'] = 'Purchased By'; | |
return $columns; | |
} | |
/** | |
* Output custom columns for coupons. | |
* @param string $column | |
*/ | |
function show_purchased_by_render_shop_order_columns( $column ) { | |
if ( $column == 'purchased_by' ) { | |
$order = new WC_Order( get_the_ID() ); | |
$first_name = $order->billing_first_name; | |
$last_name = $order->billing_last_name; | |
$customer_name = $first_name . ' ' . $last_name; | |
$customer = get_userdata($order->customer_user); | |
$name = $customer->display_name; | |
if ( !empty( $customer_name ) ) { | |
echo '<a href="'. get_edit_user_link( $order->ID ) .'">'. esc_attr($name ) .'</a>'; | |
} else { | |
echo '<span class="na">–</span>'; | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment