Last active
November 11, 2025 16:25
-
-
Save webdados/1f5c13bfc021fb82a1e20014b3fa48c9 to your computer and use it in GitHub Desktop.
Filter results on Shop as Client PRO autocomplete search
This file contains hidden or 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
| <?php | |
| add_filter( 'shop_as_client_autocomplete_results', function ( $results, $search_term ) { | |
| // Loop through results and modify/remove them | |
| foreach( $results as $key => $result ) { | |
| // Get the type of result and its ID, as $key is sequencial and not related to the result | |
| $result_value = explode( ':', $result['value'] ); | |
| switch( $result_value[0] ) { | |
| case 'user': | |
| $user_id = intval( $result_value[1] ); | |
| // example: remove this user from results if its role is 'subscriber' | |
| $user = get_user_by( 'ID', $user_id ); | |
| if ( in_array( 'subscriber', (array) $user->roles, true ) ) { | |
| unset( $results[ $key ] ); | |
| } | |
| break; | |
| case 'order': | |
| $order_id = intval( $result_value[1] ); | |
| // example: remove this order from results if its total is below 50 | |
| $order = wc_get_order( $order_id ); | |
| if ( $order && $order->get_total() < 50 ) { | |
| unset( $results[ $key ] ); | |
| } | |
| break; | |
| } | |
| } | |
| return $results; | |
| }, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment