我在之前的问题之后还有一个问题:允许在 WooCommerce 管理订单列表中使用帐单电子邮件进行搜索
以下是我的代码,用于将电子邮件和电子邮件显示为 md5、电话号码和支付网关名称:
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_md5_email'] = __( 'MD5 Email Address', 'my-textdomain' );
$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_md5_email' === $column ) {
if ( $the_order->has_status( array('completed') ) ) {
$email_address = $the_order->get_billing_email();
$email_address = md5($email_address);
echo '<span class="order_complete"> '.$email_address.' </span>';
} else {
echo '<span class="order_not_complete">This Order is Not Completed!</span>';
}
}
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();
}
}
此代码还用于过滤管理订单页面上的搜索:
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;
}
现在我遇到的问题是,当我在管理订单页面中搜索值时,如果我搜索电子邮件的MD5,它将显示在结果中,就像简单的电话号码和电子邮件以及付款名称一样网关,显示没有任何问题。
我认为以下2种方法适用:
$meta_keys[] = '_billing_email';
应更改为该值==>$meta_keys[] = md5('_billing_email');
)。如果您能在这两种情况下指导我并举个例子,我将不胜感激。
首先,结帐后创建订单时,您需要将电子邮件 md5 保存为自定义元数据:
add_action( 'woocommerce_checkout_create_order', 'save_billing_email_md5', 20, 2 );
function save_billing_email_md5( $order, $data ) {
if( $billing_email = $order->get_billing_email() ) {
$order->add_meta_data('_billing_email_md5', md5($billing_email));
}
}
然后,当在管理中手动创建/更新订单时,添加以下内容:
add_action( 'woocommerce_process_shop_order_meta', 'admin_save_billing_email_md5' );
function admin_save_billing_email_md5( $order_id ) {
if( $billing_email = get_post_meta($order_id, '_billing_email', true) ) {
update_post_meta($order_id, '_billing_email_md5', md5($billing_email));
}
}
然后将函数
extending_admin_orders_search_field()
替换为:
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';
$meta_keys[] = '_billing_email_md5'; // <=== HERE
return $meta_keys;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
这仅适用于将账单电子邮件 md5 设置为自定义元数据的订单。