Created
November 6, 2019 16:21
-
-
Save mattiasghodsian/8d0c2f3a9cdeb86020f4f67afe9452a3 to your computer and use it in GitHub Desktop.
[Woocommerce] Custom user column & searchable meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create user column | |
add_filter( 'manage_users_columns', 'new_modify_user_table' ); | |
function new_modify_user_table( $column ) | |
{ | |
$column['kundnummer'] = 'Kundnummer'; | |
return $column; | |
} | |
// Add content to user column | |
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 ); | |
function new_modify_user_table_row( $val, $column_name, $user_id ) | |
{ | |
switch ($column_name) { | |
case 'kundnummer' : | |
return get_user_meta( $user_id, 'woocommerce_customer_id', true ); | |
break; | |
default: | |
} | |
return $val; | |
} | |
// Searching Meta Data | |
add_action('pre_user_query','smd_pre_user_search'); | |
function smd_pre_user_search($user_search) | |
{ | |
global $wpdb; | |
if (!isset($_GET['s'])) return; | |
$search_array = array("customer_id", "woocommerce_customer_id", "first_name", "last_name"); | |
$user_search->query_from .= " INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID={$wpdb->usermeta}.user_id AND ("; | |
for($i=0;$i<count($search_array);$i++) { | |
if ($i > 0) $user_search->query_from .= " OR "; | |
$user_search->query_from .= "{$wpdb->usermeta}.meta_key='" . $search_array[$i] . "'"; | |
} | |
$user_search->query_from .= ")"; | |
$custom_where = $wpdb->prepare("{$wpdb->usermeta}.meta_value LIKE '%s'", "%" . $_GET['s'] . "%"); | |
$user_search->query_where = str_replace('WHERE 1=1 AND (', "WHERE 1=1 AND ({$custom_where} OR ",$user_search->query_where); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment