Fix: update REST API v3 reports/orders/totals endpoint to be compatible with HPOS (#46715)

* add a failing test for order totals with HPOS enabled but sync disabled

* fix comment

* ensure the wc/v3/reports/orders/totals endpoint is compatible with HPOS

* add changelog file

* address linter issues
This commit is contained in:
Leif Singer 2024-04-18 17:49:19 +02:00 committed by GitHub
parent af53de9aaa
commit 2d837ac687
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Ensure the wc/v3/reports/orders/totals endpoint is compatible with HPOS.

View File

@ -8,6 +8,8 @@
* @since 3.5.0
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
defined( 'ABSPATH' ) || exit;
/**
@ -39,18 +41,18 @@ class WC_REST_Report_Orders_Totals_Controller extends WC_REST_Reports_Controller
* @return array
*/
protected function get_reports() {
$totals = wp_count_posts( 'shop_order' );
$totals = OrderUtil::get_count_for_type( 'shop_order' );
$data = array();
foreach ( wc_get_order_statuses() as $slug => $name ) {
if ( ! isset( $totals->$slug ) ) {
if ( ! array_key_exists( $slug, $totals ) ) {
continue;
}
$data[] = array(
'slug' => str_replace( 'wc-', '', $slug ),
'name' => $name,
'total' => (int) $totals->$slug,
'total' => (int) $totals[ $slug ],
);
}

View File

@ -6,8 +6,16 @@
* @since 3.5.0
*/
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\RestApi\UnitTests\HPOSToggleTrait;
/**
* WC_Tests_API_Reports_Orders_Totals.
*/
class WC_Tests_API_Reports_Orders_Totals extends WC_REST_Unit_Test_Case {
use HPOSToggleTrait;
/**
* Setup our test server, endpoints, and user info.
*/
@ -31,7 +39,7 @@ class WC_Tests_API_Reports_Orders_Totals extends WC_REST_Unit_Test_Case {
}
/**
* Test getting all product reviews.
* Test getting order totals.
*
* @since 3.5.0
*/
@ -60,6 +68,45 @@ class WC_Tests_API_Reports_Orders_Totals extends WC_REST_Unit_Test_Case {
$this->assertEquals( $data, $report );
}
/**
* Test getting order totals with HPOS enabled and some orders pending sync.
*
* @since 8.9.0
*/
public function test_get_reports_with_hpos_enabled_and_sync_off() {
$this->toggle_cot_authoritative( true );
$this->disable_cot_sync();
wp_set_current_user( $this->user );
// Create some orders with HPOS enabled.
$order_counts = array(
'wc-pending' => 3,
'wc-processing' => 2,
'wc-on-hold' => 1,
);
foreach ( $order_counts as $status => $count ) {
for ( $i = 0; $i < $count; $i++ ) {
$order = OrderHelper::create_order();
$order->set_status( $status );
$order->save();
}
}
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' ) );
$report = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( wc_get_order_statuses() ), count( $report ) );
foreach ( $report as $data ) {
if ( array_key_exists( 'wc-' . $data['slug'], $order_counts ) ) {
$this->assertEquals( $order_counts[ 'wc-' . $data['slug'] ], $data['total'], 'Status: ' . $data['slug'] );
} else {
$this->assertEquals( 0, $data['total'], 'Status: ' . $data['slug'] );
}
}
}
/**
* Tests to make sure product reviews cannot be viewed without valid permissions.
*