0

I'm adding the following custom columns and they both appear:

function add_order_new_column_header( $columns ) {

    $new_columns = array();

    foreach ( $columns as $column_name => $column_info ) {

        $new_columns[ $column_name ] = $column_info;

        if ( 'order_total' === $column_name ) {
            $new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
            $new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
        }
    }

    return $new_columns;

Then I'm adding the following to generate data:

add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20);
add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content' );
function add_wc_order_admin_list_column_content( $column ) {
   
    global $post;
 
    if ( 'order_shipping' === $column ) {
 
        $order = wc_get_order( $post->ID );
        echo $order->get_shipping_method();
        
    if ( 'order_payment' === $column ) {
        $order = wc_get_order( $post->ID );
        echo $order->get_payment_method_title();
    }
    }
}

order_shipping is showing correct shipping method, but order_payment is empty. I'm not using multiple languages on the site. WooCommerce 4.1.1, PHP 7.3.19

view from WooCommerce orders admin panel:
view from WooCommerce orders admin panel

I've tried both get_payment_method and get_payment_method_list. I have orders with BACS mostly and a few Cash on Pickup which is disabled for now. What am I missing here?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

There are some mistakes and missing things in your code, try the following instead:

add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
function add_custom_columns_to_admin_orders( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;

        if ( 'order_total' === $column_name ) {
            $new_columns['order_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
            $new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
        }
    }
    return $new_columns;
}


add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders' );
function custom_columns_content_in_admin_orders( $column ) {
    global $post, $the_order;

    if ( 'order_shipping' === $column ) {
        echo $the_order->get_shipping_method();
    }
    if ( 'order_payment' === $column ) {
        echo $the_order->get_payment_method_title();
    }
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    It does!! :) Thank you so much! Should've at least seen the missing closing }. – Karl Helberg Jun 22 '20 at 23:01
  • @KarlHelberg and also `global $the_order;` better than `$order = wc_get_order( $post->ID );` – LoicTheAztec Jun 22 '20 at 23:14
  • What should I do if I want to display each customer's email instead of `get_shipping_method()'? – Adam Luper Aug 18 '23 at 10:53
  • 1
    @AdamLuper use `echo $the_order->get_billing_email();` and replace for one of the column the slugs (for example `'order_shipping'`) with `'billing_email'` in both functions. see [How to get WooCommerce order details](https://stackoverflow.com/a/44708344/3730754). – LoicTheAztec Aug 18 '23 at 10:58
  • It was great and thank you An unrelated question: Is it possible to make the e-mails displayed in this section without links, that is, when we click on the e-mails (only e-mails), it does not go to the order page or is not linked anywhere? – Adam Luper Aug 18 '23 at 11:06
  • 1
    @AdamLuper In admin orders list, there is a live jQuery click event on all `` tags of an order row (so all cells), that trigger the order single page. So to avoid that, simply use an input text field like `echo '';`. This will avoid this problem and will allow you to select and copy the email. – LoicTheAztec Aug 18 '23 at 11:31
  • thank you It was a smart solution, but again, when double-clicking or single-clicking on input, it goes to the order page, but there is no problem with selecting the text and right-clicking, if this problem could be completely fixed, it would be great. To sort this column, for example (Tarnemeetod), for example, I want it to be the second column? And another question, if the columns can be sorted, such as the order, date or total columns, what should be done? "+1" – Adam Luper Aug 18 '23 at 13:02
  • 1
    Then may be remove `readonly`. – LoicTheAztec Aug 18 '23 at 13:08
  • This method didn't work, but it doesn't matter and thank you To sort this column (Tarnemeetod), for example, I want it to be the second column? And another question, if the columns can be sorted, such as the order, date or total columns, what should be done? (There is a button to sort the column on the date and... columns) – Adam Luper Aug 18 '23 at 14:00