Commit 37b245db80b for woocommerce
commit 37b245db80baa1e3bf4d96e331360e12e0dce174
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date: Wed Dec 2 12:39:05 2026 +0000
Make stock report default sort stable so exports don't skip or repeat products (#71155)
diff --git a/plugins/woocommerce/changelog/fix-stock-report-stable-sort b/plugins/woocommerce/changelog/fix-stock-report-stable-sort
new file mode 100644
index 00000000000..5e3dc398425
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-stock-report-stable-sort
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix stock report exports that could skip or repeat products when many share the same stock status and quantity.
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 66fa4780039..023c5aa452e 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -41152,7 +41152,7 @@ parameters:
-
message: '#^Call to an undefined method object\:\:get\(\)\.$#'
identifier: method.notFound
- count: 7
+ count: 6
path: src/Admin/API/Reports/Stock/Controller.php
-
@@ -41257,12 +41257,6 @@ parameters:
count: 1
path: src/Admin/API/Reports/Stock/Controller.php
- -
- message: '#^Part \$order \(array\|string\) of encapsed string cannot be cast to string\.$#'
- identifier: encapsedStringPart.nonString
- count: 6
- path: src/Admin/API/Reports/Stock/Controller.php
-
-
message: '#^@param Automattic\\WooCommerce\\Admin\\API\\Reports\\Stock\\Stats\\WP_REST_Response \$response does not accept actual type of parameter\: WP_REST_Response\.$#'
identifier: parameter.phpDocType
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php
index bbc2bc408cd..0a38fcbae50 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php
@@ -271,20 +271,20 @@ class Controller extends GenericController implements ExportableInterface {
global $wpdb;
$orderby = $wp_query->get( 'orderby' );
- $order = esc_sql( $wp_query->get( 'order' ) ? $wp_query->get( 'order' ) : 'desc' );
+ $order = 'ASC' === strtoupper( (string) $wp_query->get( 'order' ) ) ? 'ASC' : 'DESC';
switch ( $orderby ) {
case 'stock_quantity':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
- $args['orderby'] = " wc_product_meta_lookup.stock_quantity {$order}, wc_product_meta_lookup.product_id {$order} ";
+ $args['orderby'] = " wc_product_meta_lookup.stock_quantity {$order}, {$wpdb->posts}.ID {$order} ";
break;
case 'stock_status':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
- $args['orderby'] = " wc_product_meta_lookup.stock_status {$order}, wc_product_meta_lookup.stock_quantity {$order} ";
+ $args['orderby'] = " wc_product_meta_lookup.stock_status {$order}, wc_product_meta_lookup.stock_quantity {$order}, {$wpdb->posts}.ID {$order} ";
break;
case 'sku':
$args['join'] = self::append_product_sorting_table_join( $args['join'] );
- $args['orderby'] = " wc_product_meta_lookup.sku {$order}, wc_product_meta_lookup.product_id {$order} ";
+ $args['orderby'] = " wc_product_meta_lookup.sku {$order}, {$wpdb->posts}.ID {$order} ";
break;
}
diff --git a/plugins/woocommerce/tests/php/src/Admin/API/Reports/Stock/ControllerTest.php b/plugins/woocommerce/tests/php/src/Admin/API/Reports/Stock/ControllerTest.php
index be07305c7ff..5a62c8f9db1 100644
--- a/plugins/woocommerce/tests/php/src/Admin/API/Reports/Stock/ControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/API/Reports/Stock/ControllerTest.php
@@ -96,6 +96,44 @@ class ControllerTest extends WC_REST_Unit_Test_Case {
$this->assertSame( $expected, $this->get_report( $ids ), 'The stock report lists the wrong products in an admin context.' );
}
+ /**
+ * Products without a lookup row have NULL in every lookup column, so only the post ID can order them.
+ *
+ * @testdox Should order products without a lookup row by ID when sorting by $orderby.
+ * @dataProvider provide_lookup_table_orderby
+ *
+ * @param string $orderby Sort column.
+ */
+ public function test_products_without_lookup_row_are_ordered_by_id( string $orderby ): void {
+ global $wpdb;
+
+ $ids = array();
+ for ( $i = 0; $i < 3; $i++ ) {
+ $ids[] = $this->create_product( ProductStatus::PUBLISH )->get_id();
+ }
+
+ foreach ( $ids as $id ) {
+ $wpdb->delete( $wpdb->wc_product_meta_lookup, array( 'product_id' => $id ) );
+ }
+
+ rsort( $ids );
+
+ $this->assertSame( $ids, $this->get_sorted_ids( $ids, $orderby, 'desc' ) );
+ }
+
+ /**
+ * Sort columns that read from the product lookup table.
+ *
+ * @return array
+ */
+ public function provide_lookup_table_orderby(): array {
+ return array(
+ 'stock_quantity' => array( 'stock_quantity' ),
+ 'stock_status' => array( 'stock_status' ),
+ 'sku' => array( 'sku' ),
+ );
+ }
+
/**
* @testdox Should leave out the stock notification columns and sorts when the feature is disabled.
*/
diff --git a/plugins/woocommerce/tests/php/src/Admin/ReportExporterTest.php b/plugins/woocommerce/tests/php/src/Admin/ReportExporterTest.php
index ddad110d142..55a8c5f39e8 100644
--- a/plugins/woocommerce/tests/php/src/Admin/ReportExporterTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/ReportExporterTest.php
@@ -814,6 +814,13 @@ class ReportExporterTest extends WC_Unit_Test_Case {
$this->assertCount( 4, $this->recorded_actions( $export_hook ), 'The last page must not queue another one.' );
$this->assertSame( $this->report_skus(), $this->exported_skus( 'chain' ), 'Every row must be exported, in report order.' );
+ $request = new \WP_REST_Request( 'GET', '/wc-analytics/reports/stock' );
+ $request->set_param( 'per_page', 100 );
+ $report_ids = array_column( rest_do_request( $request )->get_data(), 'id' );
+ $sorted_ids = $report_ids;
+ sort( $sorted_ids );
+ $this->assertSame( $sorted_ids, $report_ids, 'Products with the same stock status and quantity must be listed by product ID, so every page sees the same order.' );
+
$next_pages = wp_list_filter( $this->queue->get_methods_called(), array( 'hook' => $export_hook ) );
$this->assertSame(
array( 'schedule_single', 'add', 'add', 'add' ),