Skip custom search for HPOS API queries as it's handled already. (#36213)

* Skip custom search for HPOS API queries as it's handled already.

For HPOS, we don't need a seperate step of passing the args through wc_order_search since we take care of search params in the final query itself. This duplication was actually causing for some results to not be displayed since we were ending up not honouring pagination arguments and would search only in first 10 matches.

* Add changelog.
This commit is contained in:
Vedanshu Jain 2023-01-05 01:38:22 +05:30 committed by GitHub
parent e84c38504f
commit 584167746e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Skip custom search for HPOS API queries as it's handled already.

View File

@ -8,8 +8,11 @@
* @since 2.6.0
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
defined( 'ABSPATH' ) || exit;
// phpcs:disable Squiz.Classes.ClassFileName.NoMatch, Squiz.Classes.ValidClassName.NotCamelCaps -- Legacy class name, can't change without breaking backward compat.
/**
* REST API Orders controller class.
*
@ -18,6 +21,7 @@ defined( 'ABSPATH' ) || exit;
*/
class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
// phpcs:enable
/**
* Endpoint namespace.
*
@ -346,7 +350,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
// Only fetch fields that we need.
$fields = $this->get_fields_for_response( $this->request );
foreach ( $dependent_fields as $field_key => $dependency ) {
if ( in_array( $field_key, $fields ) && ! in_array( $dependency, $fields ) ) {
if ( in_array( $field_key, $fields, true ) && ! in_array( $dependency, $fields, true ) ) {
$fields[] = $dependency;
}
}
@ -506,6 +510,8 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
* @param WP_REST_Response $response The response object.
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
*
* @since 4.5.0
*/
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
}
@ -594,7 +600,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
}
// Search.
if ( ! empty( $args['s'] ) ) {
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() && ! empty( $args['s'] ) ) {
$order_ids = wc_order_search( $args['s'] );
if ( ! empty( $order_ids ) ) {
@ -610,6 +616,8 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*
* @since 4.5.0.
*/
$args = apply_filters( 'woocommerce_rest_orders_prepare_object_query', $args, $request );
@ -693,6 +701,8 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
* @param WC_Data $order Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
*
* @since 4.5.0.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating );
}
@ -979,6 +989,14 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
// Prepare item data.
$item = $this->$method( $posted, $action, $item );
/**
* Allow extensions be notified before the item before is saved.
*
* @param WC_Order_Item $item The item object.
* @param array $posted The item data.
*
* @since 4.5.0.
*/
do_action( 'woocommerce_rest_set_order_item', $item, $posted );
// If creating the order, add the item to it.