3

Based on Add columns to admin orders list in WooCommerce answer code, I was able to add 3 custom columns to Admin Orders list:

  • billing email,
  • billing phone,
  • payment method title.

My Code:

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_email'] = __( 'Email Address', 'my-textdomain' );
            $new_columns['order_customers_phone'] = __( 'Customers Phone', 'my-textdomain' );
            $new_columns['order_payment'] = __( 'Payment Method', '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_email' === $column ) {
        if ( $the_order->has_status( array('completed') ) ) {
            $email_address = $the_order->get_billing_email();
            echo '<span class="order_complete"> '.$email_address.' </span>';
        } else {
            echo '<span class="order_not_complete">This Order is Not Completed!</span>';
        }
    }
    
    if ( 'order_customers_phone' === $column ) {
        echo '<label for="billing_phone" class="label-billing-email">Customer Phone Number:  </label>';
        echo '<input type="text" name="billing_phone" value="' . $the_order->get_billing_phone() . '" readonly />';
    }
    
    if ( 'order_payment' === $column ) {
        echo $the_order->get_payment_method_title();
    }
}

How to use the search field with the billing email, or the billing phone, or the payment method title?

enter image description here

2
  • @LoicTheAztec I would appreciate it if you could tell me what exactly my-textdomain is used for in the first function? Commented Sep 29, 2023 at 19:29
  • "my-textdomain" is to be replaced with the related plugin text domain slug like for example "woocommerce". It is used for string translations in multilingual websites. Commented Sep 29, 2023 at 20:51

1 Answer 1

3

Based on this answer, the following will make Admin Orders searchable via:

  • the billing email,
  • the billing phone,
  • or the payment method title.
add_filter( 'woocommerce_shop_order_search_fields', 'extending_admin_orders_search_field', 10, 1 );
function extending_admin_orders_search_field( $meta_keys ){
    $meta_keys[] = '_billing_email';
    $meta_keys[] = '_billing_phone';
    $meta_keys[] = '_payment_method_title';

    return $meta_keys;
}

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

Sign up to request clarification or add additional context in comments.

10 Comments

It was wonderful as always. If, for example, in this condition, the code if ( 'order_email' === $column ) has displayed the email as md5, how should it be handled for searching? (What meta keys should I put? Because the email is not normal and the email is md5!) $meta_keys[] = '_billing_email'; $meta_keys[] = '_billing_phone'; $meta_keys[] = '_payment_method_title';
I mostly want to know how to search for custom columns like this link in my question. ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST
You will use $meta_keys[] = '_the_meta_key1'; and $meta_keys[] = '_the_meta_key2';
Admin orders search is made on database, so you can only use existing metadata located in wp_postmeta table for orders to perform a search on the displayed orders, when using my code answer. That said, you can always add some custom metadata on order creation, to extend the search, for example.
"That said, you can always add some custom metadata on order creation, to extend the search, for example."…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.