Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add support for WooCommerce Admin filters when searching Orders with SearchWP
<?php
// 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
You can’t perform that action at this time.