Add filter hooks for HPOS order search to support custom search filters (#45954)

This allows adding new order search methods, or remove existing ones via filter.
This commit is contained in:
Vedanshu Jain 2024-03-30 00:09:45 +05:30 committed by GitHub
parent e3b1dd6a10
commit 51fd34350c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 10 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: enhancement
Add filters to support adding custom search methods in HPOS admin and remember the last used search option

View File

@ -1352,7 +1352,7 @@ class ListTable extends WP_List_Table {
}
do_action( 'woocommerce_remove_order_personal_data', $order ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$changed++;
++$changed;
}
return $changed;
@ -1380,7 +1380,7 @@ class ListTable extends WP_List_Table {
$order->update_status( $new_status, __( 'Order status changed by bulk edit.', 'woocommerce' ), true );
do_action( 'woocommerce_order_edit_status', $id, $new_status ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$changed++;
++$changed;
}
return $changed;
@ -1403,7 +1403,7 @@ class ListTable extends WP_List_Table {
$updated_order = wc_get_order( $id );
if ( ( $force_delete && false === $updated_order ) || ( ! $force_delete && $updated_order->get_status() === 'trash' ) ) {
$changed++;
++$changed;
}
}
@ -1423,7 +1423,7 @@ class ListTable extends WP_List_Table {
foreach ( $ids as $id ) {
if ( $orders_store->untrash_order( wc_get_order( $id ) ) ) {
$changed++;
++$changed;
}
}
@ -1634,10 +1634,25 @@ class ListTable extends WP_List_Table {
'products' => __( 'Products', 'woocommerce' ),
'all' => __( 'All', 'woocommerce' ),
);
/**
* Filters the search filters available in the admin order search. Can be used to add new or remove existing filters.
* When adding new filters, `woocommerce_hpos_generate_where_for_search_filter` should also be used to generate the WHERE clause for the new filter
*
* @since 8.9.0.
*
* @param $options array List of available filters.
*/
$options = apply_filters( 'woocommerce_hpos_admin_search_filters', $options );
$saved_setting = get_user_setting( 'wc-search-filter-hpos-admin', 'all' );
$selected = sanitize_text_field( wp_unslash( $_REQUEST['search-filter'] ?? $saved_setting ) );
if ( $saved_setting !== $selected ) {
set_user_setting( 'wc-search-filter-hpos-admin', $selected );
}
?>
<select name="search-filter" id="order-search-filter">
<?php foreach ( $options as $value => $label ) { ?>
<option value="<?php echo esc_attr( wp_unslash( sanitize_text_field( $value ) ) ); ?>" <?php selected( $value, sanitize_text_field( wp_unslash( $_REQUEST['search-filter'] ?? 'all' ) ) ); ?>><?php echo esc_html( $label ); ?></option>
<option value="<?php echo esc_attr( wp_unslash( sanitize_text_field( $value ) ) ); ?>" <?php selected( $value, sanitize_text_field( wp_unslash( $selected ) ) ); ?>><?php echo esc_html( $label ); ?></option>
<?php
}
}

View File

@ -52,7 +52,7 @@ class OrdersTableSearchQuery {
* @return array Array of search filters.
*/
private function sanitize_search_filters( string $search_filter ) : array {
$available_filters = array(
$core_filters = array(
'order_id',
'customer_email',
'customers', // customers also searches in meta.
@ -60,9 +60,9 @@ class OrdersTableSearchQuery {
);
if ( 'all' === $search_filter || '' === $search_filter ) {
return $available_filters;
return $core_filters;
} else {
return array_intersect( $available_filters, array( $search_filter ) );
return array( $search_filter );
}
}
@ -119,7 +119,29 @@ class OrdersTableSearchQuery {
LEFT JOIN $items_table AS search_query_items ON search_query_items.order_id = $orders_table.id
";
}
return '';
/**
* Filter to support adding a custom order search filter.
* Provide a JOIN clause for a new search filter. This should be used along with `woocommerce_hpos_admin_search_filters`
* to declare a new custom filter, and `woocommerce_hpos_generate_where_for_search_filter` to generate the WHERE
* clause.
*
* Hardcoded JOINS (products) cannot be modified using this filter for consistency.
*
* @since 8.9.0
*
* @param string $join The JOIN clause.
* @param string $search_term The search term.
* @param string $search_filter The search filter. Use this to bail early if this is not filter you are interested in.
* @param OrdersTableQuery $query The order query object.
*/
return apply_filters(
'woocommerce_hpos_generate_join_for_search_filter',
'',
$this->search_term,
$search_filter,
$this->query
);
}
/**
@ -189,7 +211,28 @@ class OrdersTableSearchQuery {
return "`$order_table`.id IN ( $meta_sub_query ) ";
}
return '';
/**
* 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
);
}
/**