本篇文章更新時間:2023/05/26
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣 或 新台幣 贊助支持。
寫這篇就不得不說 WordPress 在「使用者」這塊後台工具其實還.. 很原始,預設沒有太多可以調整的地方。尤其直覺上應該是顯示「姓名」的那個「顯示名稱」欄位,其實固定是用兩個欄位「姓氏」與「名字」去組合出來的,中文使用者明顯會看到姓名中間有一個空格。
如果對 WooCommerce 使用者權限的調整配置有興趣可以參考先前的這篇筆記:[WooCommerce] 後台整理術:開放修改作者、編輯等使用者帳號設定
更不用說直覺上搜尋應該要「看得到的都要可以找得到」,但... 沒有,你沒辦法用「姓氏」或是「名字」的關鍵字來找,而 WooCommerce 結帳欄位那些這麼豐富的顧客資訊自然也沒一個可以!
從預設的 Query 來看:
SELECT SQL_CALC_FOUND_ROWS wp_users.ID
FROM wp_users
WHERE 1=1
AND (user_login LIKE '%keyword%'
OR user_url LIKE '%keyword%'
OR user_email LIKE '%keyword%'
OR user_nicename LIKE '%keyword%'
OR display_name LIKE '%keyword%'
OR display_name LIKE '%keyword%')
ORDER BY user_login ASC
LIMIT 0, 210
如果在不動搜尋 Query 的情況下,就是想辦法把「姓名」一起塞到 display_name
欄位裡,就至少可以達成針對使用者姓名的搜尋。
使用客製化欄位資訊搜尋使用者的方法
不然就是要客製化這個搜尋條件,如下:
function mxp_add_custom_fields_for_user_search($user_query) {
global $wpdb;
if (isset($user_query->query_vars['search']) && !empty($user_query->query_vars['search']) && is_admin() && isset($_REQUEST['s']) && !empty($_REQUEST['s'])) {
$search_term = str_replace('*', '%', $user_query->query_vars['search']);
$user_query->query_from .= " JOIN {$wpdb->usermeta} usrmeta ON usrmeta.user_id = {$wpdb->users}.ID";
// 列出想加入搜尋條件的客製化欄位
$custom_fields = array(
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_company',
'billing_country',
'billing_email',
'billing_first_name',
'billing_last_name',
'billing_phone',
'billing_postcode',
'billing_state',
'billing_video_playback_gmail',
'description',
'first_name',
'last_name',
'nickname',
'shipping_address_1',
'shipping_address_2',
'shipping_city',
'shipping_company',
'shipping_country',
'shipping_email',
'shipping_first_name',
'shipping_last_name',
'shipping_method',
'shipping_phone',
'shipping_postcode',
'shipping_state',
);
$where = array();
foreach ($custom_fields as $key => $field) {
$where[] = "usrmeta.meta_key LIKE '" . esc_sql($field) . "' AND usrmeta.meta_value LIKE '" . esc_sql($search_term) . "'";
}
// 把條件組合起來
$user_query->query_where .= ' OR (' . implode(' OR ', $where) . ')';
}
}
add_action('pre_user_query', 'mxp_add_custom_fields_for_user_search', 11, 1);
這樣就是可以把 WooCommerce 結帳欄位填寫的資料也納入後台使用者搜尋條件。
修改使用者欄位的順序和數值
再來就是如果想用這些欄位的資料,加入使用者頁面中的表格欄位中的話,可以搭配下面程式碼片段實現:
function mxp_custom_user_column_headers($columns) {
$columns = array(); //全部重新排序
$columns['cb'] = '';
$columns['username'] = '使用者名稱';
//$columns['name'] = '顯示名稱';
$columns['mxp_display_name'] = '使用者姓名';
$columns['email'] = '電子郵件地址';
$columns['role'] = '使用者角色';
$columns['posts'] = '文章';
return $columns;
}
add_filter('manage_users_columns', 'mxp_custom_user_column_headers', 11, 1);
function mxp_custom_user_column_content($value, $column_name, $user_id) {
if ($column_name === 'mxp_display_name') {
$user_info = get_userdata($user_id);
$mxp_display_name = $user_info->display_name;
$value = $mxp_display_name;
}
return $value;
}
add_filter('manage_users_custom_column', 'mxp_custom_user_column_content', 10, 3);
mxp_custom_user_column_headers
方法用來處理列表中欄位標題與排序。要重新排序管理、移除某個欄位或新增某個欄位都在這邊處理。
而 mxp_custom_user_column_content
方法則是用來輸出對應欄位的欄位值方法,就看你的資料來源來處理回傳的欄位資料,範例就是上述提到的「把姓名都放到 display_name
欄位裡」並且把預設的「顯示名稱」移除,的一種實作方式。
要加入顯示 WooCommerce 欄位資訊也沒問題,比照前段的說明補上欄位標題與取值方法就可以了!
後記
這樣算是一個基本的整理起頭。其他我通常還會搭配:
- Recently Registered 這套顯示使用者註冊時間的工具
- User Switching 管理員配置權限時不用登出與登入客戶帳號的方便切換顯示工具
其他更細節客戶指定的欄位功能像是顯示消費次數、匯出顧客過往訂單等等的就非常態功能不一一寫了。