{lang: 'hu'}

In wordpress it’s pretty easy to add a new column to a post listing table. Let’s take a look how it works with the woocommerce orders table!


function add_custom_columns_to_orders_table($columns) {
    $new_columns = $columns;

    if(!is_array($columns)) {
    	$columns = array();
    }

    unset( $new_columns['order_actions'] );

    $new_columns['shipping_method'] = __('Shipping method', 'your-domain');
    $new_columns['payment_method'] = __('Payment method', 'your-domain');

    // We created a new array and put the new columns before
    // the order_actions column in order to not be displayed
    // as last columns, because it looks weird
    $new_columns['order_actions'] = $columns['order_actions'];

    return $new_columns;
}

// With this filter we can register the "header" part
add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_orders_table', 11 );

And of course we need to tell wordpress where the new cells’ values come from:


function get_custom_columns_values($column) {
    global $post;
    
    $order_facory = new WC_Order_Factory();
    $order = $order_facory->get_order( $post->ID );

    if ( $column == 'shipping_method' ) {    
        echo $order->get_shipping_method();
    }
    if ( $column == 'payment_method' ) {    
        echo $order->payment_method_title;
    }
}

add_action( 'manage_shop_order_posts_custom_column', 'get_custom_columns_values', 2 );

That’s all! It wasn’t too difficult, was it? In the next episode I’m going to make these columns sortable.