Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableSearchQuery::generate_where_for_search_filter
Generates WHERE clause for a given search filter. Right now we only have the products and customers filters that actually use WHERE, but in the future we may add more -- for example, custom order fields, payment tokens and so on. This function makes it easier to add more filters in the future.
Method of the class: OrdersTableSearchQuery{}
Hooks from the method
Returns
String. WHERE clause.
Usage
// private - for code of main (parent) class only $result = $this->generate_where_for_search_filter( $search_filter ): string;
- $search_filter(string) (required)
- Name of the search filter.
OrdersTableSearchQuery::generate_where_for_search_filter() OrdersTableSearchQuery::generate where for search filter code WC 10.4.3
private function generate_where_for_search_filter( string $search_filter ): string {
global $wpdb;
$order_table = $this->query->get_table_name( 'orders' );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $order_table is hardcoded.
if ( 'customer_email' === $search_filter ) {
return $wpdb->prepare(
"`$order_table`.billing_email LIKE %s",
$wpdb->esc_like( $this->search_term ) . '%'
);
}
if ( 'order_id' === $search_filter && is_numeric( $this->search_term ) ) {
return $wpdb->prepare(
"`$order_table`.id = %d",
absint( $this->search_term )
);
}
if ( 'transaction_id' === $search_filter ) {
return $wpdb->prepare(
"`$order_table`.transaction_id LIKE %s",
'%' . $wpdb->esc_like( $this->search_term ) . '%'
);
}
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( 'products' === $search_filter ) {
return $this->get_where_for_products();
}
if ( 'customers' === $search_filter ) {
return $this->get_where_for_customers();
}
/**
* Filter to support adding a custom order search filter.
* Provide a WHERE clause for a custom search filter via this filter. This should be used with the
* `woocommerce_hpos_admin_search_filters` to declare a new custom filter, and optionally also with the
* `woocommerce_hpos_generate_join_for_search_filter` filter if a join is also needed.
*
* Hardcoded filters (products, customers, ID and email) cannot be modified using this filter for consistency.
*
* @since 8.9.0
*
* @param string $where WHERE clause to add to the search query.
* @param string $search_term The search term.
* @param string $search_filter Name of the search filter. Use this to bail early if this is not the filter you are looking for.
* @param OrdersTableQuery $query The order query object.
*/
return apply_filters(
'woocommerce_hpos_generate_where_for_search_filter',
'',
$this->search_term,
$search_filter,
$this->query
);
}