Skip to content

Commit

Permalink
Fix: update REST API v3 reports/orders/totals endpoint to be compatib…
Browse files Browse the repository at this point in the history
…le 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
  • Loading branch information
lsinger committed Apr 18, 2024
1 parent af53de9 commit 2d837ac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Ensure the wc/v3/reports/orders/totals endpoint is compatible with HPOS.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @since 3.5.0
*/

use Automattic\WooCommerce\Utilities\OrderUtil;

defined( 'ABSPATH' ) || exit;

/**
Expand Down Expand Up @@ -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 ],
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -31,7 +39,7 @@ public function test_register_routes() {
}

/**
* Test getting all product reviews.
* Test getting order totals.
*
* @since 3.5.0
*/
Expand Down Expand Up @@ -60,6 +68,45 @@ public function test_get_reports() {
$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.
*
Expand Down

0 comments on commit 2d837ac

Please sign in to comment.