-->

Add columns in admin order list on woocommerce 3.3

2020-08-01 07:33发布

问题:

Related to: Add columns to admin orders list in WooCommerce backend

Is it possible to add only one new column in admin Order list instead. For the content of this column my-column1 I will like to have 2 custom fields like:

$my_var_one = get_post_meta( $post_id, '_the_meta_key1', true );

$my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );

To finish is it possible to position this new column after the order_number?

Any help is appreciated.

回答1:

Updated - It can be done this way:

// ADDING 1 NEW COLUMNS WITH THEIR TITLES
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_number' ){
            $reordered_columns['my-column1'] = __( 'Title1','theme_slug');
        }
    }
    return $reordered_columns;
}

// Adding the data for the additional column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    if( 'my-column1' == $column )
    {
        // Get custom post meta data 1
        $my_var_one = get_post_meta( $post_id, '_the_meta_key1', true );
        if(!empty($my_var_one))
            echo $my_var_one;

        // Get custom post meta data 2
        $my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );
        if(!empty($my_var_two))
            echo $my_var_two;

        // Testing (to be removed) - Empty value case
        if( empty($my_var_one) && empty($my_var_two) )
            echo '<small>(<em>no value</em>)</small>';
    }
}

The other function stays unchanged…

Code goes in function.php file of your active child theme (or active theme). Tested and works.