function add_search_to_orders() {
$allowed_roles = array(/*'administrator', */'shop_manager'); // Add or remove any other user roles here
$user = wp_get_current_user();
$user_roles = (array) $user->roles;
if (array_intersect($allowed_roles, $user_roles)) {
$show_clear_link = isset($_GET['search_orders']) && !empty($_GET['search_orders']);
echo '<form method="get" id="orders-search-form">
<input type="text" name="search_orders" placeholder="Nhập mã ID để tìm đơn hàng..." value="' . (isset($_GET['search_orders']) ? esc_attr($_GET['search_orders']) : '') . '" />
<button class="bt_sf" type="submit">Tìm ngay</button>';
if ($show_clear_link) {
echo '<a href="' . esc_url(remove_query_arg('search_orders')) . '">Reset</a>';
}
echo '</form>';
}
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');
// Make the search field function with a WooCommerce HPOS shop
function search_all_orders_by_number($query) {
if (isset($_GET['search_orders']) && !empty($_GET['search_orders'])) {
$search_term = sanitize_text_field($_GET['search_orders']);
$query['post__in'] = array($search_term);
}
return $query;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');