-
-
Save searchwpgists/d849701d5633eb3ffcb689062721aab6 to your computer and use it in GitHub Desktop.
Add support for WooCommerce Admin filters when searching Orders with SearchWP
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 | |
| // @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
| // Add support for WooCommerce Admin filters when searching Orders with SearchWP. | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| global $wpdb; | |
| if ( isset( $_GET['_customer_user'] ) && ! empty( $_GET['_customer_user'] ) ) { | |
| $mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) ); | |
| $mod->set_local_table( $wpdb->postmeta ); | |
| $mod->on( 'post_id', [ 'column' => 'id' ] ); | |
| $mod->on( 'meta_key', [ 'value' => '_customer_user' ] ); | |
| $mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) { | |
| return $wpdb->prepare( "{$runtime_mod->get_local_table_alias()}.meta_value = %d", absint( $_GET['_customer_user'] ) ); | |
| } ); | |
| $mods[] = $mod; | |
| } | |
| if ( isset( $_GET['m'] ) && ! empty( $_GET['m'] ) ) { | |
| $mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) ); | |
| $mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) { | |
| return $wpdb->prepare( "DATE_FORMAT( {$runtime_mod->get_local_table_alias()}.post_date, \"%Y%m\" ) = %d", absint( $_GET['m'] ) ); | |
| } ); | |
| $mods[] = $mod; | |
| } | |
| return $mods; | |
| }, 99, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment