如何在WooCommerce后台订单列表中按账单邮箱等字段搜索
WooCommerce后台订单列表自定义列及搜索功能扩展
你已经通过以下代码在WooCommerce后台订单列表添加了账单邮箱、账单电话、支付方式标题三个自定义列:
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(); } }
要让后台搜索框支持按账单邮箱、账单电话或支付方式标题搜索,只需添加以下代码即可:
// 扩展WooCommerce订单搜索字段,支持账单邮箱、电话、支付方式标题搜索 add_filter( 'woocommerce_shop_order_search_fields', 'extend_order_searchable_fields' ); function extend_order_searchable_fields( $default_fields ) { // 追加账单邮箱字段 $default_fields[] = '_billing_email'; // 追加账单电话字段 $default_fields[] = '_billing_phone'; // 追加支付方式标题字段 $default_fields[] = '_payment_method_title'; return $default_fields; }
代码说明
woocommerce_shop_order_search_fields是WooCommerce官方提供的过滤器,用于扩展后台订单列表的可搜索字段范围。- 我们通过这个过滤器,将账单邮箱(元键
_billing_email)、账单电话(元键_billing_phone)、支付方式标题(元键_payment_method_title)这三个字段加入到默认的搜索字段集合中。 - 添加后,在后台订单列表的搜索框中输入邮箱、电话或支付方式名称,系统就会自动匹配包含对应内容的订单。
内容的提问来源于stack exchange,提问作者SubZer0
相关产品推荐
相关产品推荐

