You can use the following code to accomplish this
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_1'] = 'סניף';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
// order id
$order_id = $post->ID;
// get Order object
$order = new WC_Order( $order_id );
// get customer user id
$user_id = $order->user_id;
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'MY_COLUMN_ID_1' && $user_id ) {
// get user groups
$groups = groups_get_user_groups( $user_id );
if ( ! empty( $groups ) && ! empty( $groups['groups'] ) ) {
// let's make a comma separated list of the group IDs
echo implode( ',', $groups['groups'] );
//if you would like to get just one group ID, uncomment the following
//echo $groups['groups'][0];
}
}
//stop editing
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'MY_COLUMN_ID_1' => 'MY_COLUMN_1_POST_META_ID'
//stop editing
);
return wp_parse_args( $custom, $columns );
}
Very nice kailanw, thank you very much!
The column show an arrow for sorting but for some reason it doesn’t really sort, just mixes the order when I click on the column name, any idea why?
Another small thing, how would you go about showing the group name instead of ID?
I have this for showing the name on the users list page of admin:
function cac_column_meta_value_group_name( $value, $object_id, $column ) {
$custom_field_key = 'field_reg_groups';
if ( 'column-meta' == $column->get_type() && $custom_field_key == $column->get_field() && $value ) {
// Value is a group ID here
if( $group = groups_get_group( array( 'group_id' => $value ) ) ){
$value = $group->name;
}
}
return $value;
}
add_filter( 'cac/column/meta/value', 'cac_column_meta_value_group_name', 10, 3 );
Hope to be able to achieve the same for the orders page (you helped a lot!!!).
Is it possible to achieve a filter option for this field in the orders list?
I forgot to subscribe to the post via email. The code below will give you the group names instead of group ID.
Unfortunately, I am not sure how to sort by Groups at the moment. The reason is because Orders are a post type and doesn’t necessarily have a relation to BuddyPress groups. So there isn’t an immediate query I can think of to make this sortable. I hope that makes sense.
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_1'] = 'סניף';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
// order id
$order_id = $post->ID;
// get Order object
$order = new WC_Order( $order_id );
// get customer user id
$user_id = $order->user_id;
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'MY_COLUMN_ID_1' && $user_id ) {
// get user groups
$groups = groups_get_user_groups( $user_id );
if ( ! empty( $groups ) && ! empty( $groups['groups'] ) ) {
$group_list = array();
foreach ( $groups['groups'] as $key => $group_id ) {
$group = groups_get_group( array( 'group_id' => $group_id ) );
$group_list[] = $group->name;
}
// let's make a comma separated list of the group IDs
echo implode( ', ', $group_list );
//if you would like to get just one group ID, uncomment the following
//echo $groups['groups'][0];
}
}
//stop editing
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'MY_COLUMN_ID_1' => 'MY_COLUMN_1_POST_META_ID'
//stop editing
);
return wp_parse_args( $custom, $columns );
}
Makes perfect sense!
Thank you very much for this help, You helped me a great deal here. Really appreciate it and hope to learn better php so that I could also understand and be able to extend wordpress and plugins soon 🙂
Should I mark this as resolved and make a new post on making a custom column which is not related to the orders post type filterable (or in the worst case sortable)?