Fix: order refunds REST API endpoint reading from posts table even with HPOS active (#36308)

Move HPOS-aware orders retrieval code from REST API v3 to v2. 

This has two effects:

1. Make the /orders endpoint compatible with REST API v2 when HPOS
   is active, additionally to v3 (v1 would require additional work).

2. Fix the /orders/<id>/refunds endpoints for v2 and v3, which was
   retrieving data from the posts table even when HPOS active

The class hierarchy is: RefundsV3 extends RefundsV2 extends OrdersV2
and OrdersV3 extends OrdersV2.

Co-authored-by: barryhughes <3594411+barryhughes@users.noreply.github.com>
This commit is contained in:
Néstor Soriano 2023-01-09 10:59:22 +01:00 committed by GitHub
parent 74768f4c56
commit b03cb80c0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 32 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix REST API order refunds enpoint when HPOS is active, and make v2 orders endpoint compatible with HPOS

View File

@ -8,10 +8,10 @@
* @since 2.6.0
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Utilities\OrderUtil;
// 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.
@ -2000,4 +2000,33 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
return $params;
}
/**
* Get objects.
*
* @param array $query_args Query args.
* @return array
*/
protected function get_objects( $query_args ) {
// Do not use WC_Order_Query for the CPT datastore.
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) {
return parent::get_objects( $query_args );
}
$query = new \WC_Order_Query(
array_merge(
$query_args,
array(
'paginate' => true,
)
)
);
$results = $query->get_orders();
return array(
'objects' => $results->orders,
'total' => $results->total,
'pages' => $results->max_num_pages,
);
}
}

View File

@ -158,6 +158,8 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @since 7.4.0
*
* @param WC_Data $order Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
@ -244,7 +246,7 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
$cpt_hidden_keys = array();
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$cpt_hidden_keys = (new \WC_Order_Data_Store_CPT())->get_internal_meta_keys();
$cpt_hidden_keys = ( new \WC_Order_Data_Store_CPT() )->get_internal_meta_keys();
}
// XXX: This might be removed once we finalize the design for internal keys vs meta vs props in COT.
@ -310,35 +312,6 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
return $args;
}
/**
* Get objects.
*
* @param array $query_args Query args.
* @return array
*/
protected function get_objects( $query_args ) {
// Do not use WC_Order_Query for the CPT datastore.
if ( ! OrderUtil::custom_orders_table_usage_is_enabled() ) {
return parent::get_objects( $query_args );
}
$query = new \WC_Order_Query(
array_merge(
$query_args,
array(
'paginate' => true,
)
)
);
$results = $query->get_orders();
return array(
'objects' => $results->orders,
'total' => $results->total,
'pages' => $results->max_num_pages,
);
}
/**
* Get the Order's schema, conforming to JSON Schema.
*