Search code examples
phpwordpresswoocommercemetadataorders

Allow search using billing email in WooCommerce Admin Orders list


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


Solution

  • 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.