Reputation: 7
I've added a new custom column in the WooCommerce admin order list:
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
// all of your columns will be added before the actions column
$new_columns['storecode'] = '門市代碼';
//stop editing
return $new_columns;
}
// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
$order_id = get_post($order_id)->ID;
$CVSStoreID = get_post_meta($order_id, '_shipping_CVSStoreID', true);
if ( $column == 'storecode' ) {
echo ( isset( $CVSStoreID ) ? $CVSStoreID : '' );
}
}
How should I move this column to the front?
Upvotes: 1
Views: 529
Reputation: 29624
Your current code will add the custom column at the end, this is because you add this column to the end of the existing $columns
array.
To change this you can use array_slice, then it is just a matter of adjusting the $number
variable to the desired position.
So you get:
// Display new column on WooCommerce admin orders list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
// Number (adapt to your needs)
$number = 4;
// Add new column after $number column
return array_slice( $columns, 0, $number, true )
+ array( 'storecode' => __( '門市代碼', 'woocommerce' ) )
+ array_slice( $columns, $number, NULL, true );
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Display details in the new column on WooCommerce admin orders list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'storecode' ) {
// Get order
$order = wc_get_order( $post_id );
// Get meta
$value = $order->get_meta( '_shipping_CVSStoreID' );
// NOT empty
if ( ! empty ( $value ) ) {
echo ucfirst( $value );
} else {
echo __( 'N/A', 'woocommerce' );
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
Upvotes: 1