diff --git a/plugins/woocommerce/changelog/fix-update-rest-api-order-totals-hpos b/plugins/woocommerce/changelog/fix-update-rest-api-order-totals-hpos new file mode 100644 index 00000000000..4b2d40bb008 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-update-rest-api-order-totals-hpos @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Ensure the wc/v3/reports/orders/totals endpoint is compatible with HPOS. diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-report-orders-totals-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-report-orders-totals-controller.php index 77c74f6167f..dd13c25bc96 100644 --- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-report-orders-totals-controller.php +++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-report-orders-totals-controller.php @@ -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 ], ); } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/reports-orders-totals.php b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/reports-orders-totals.php index 95e1ec02632..8c8cf76709a 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/reports-orders-totals.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/reports-orders-totals.php @@ -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. *