Commit c1b0cb53408 for woocommerce
commit c1b0cb53408bf93195dab64ccb89e4bdd069a308
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Fri Nov 20 10:30:12 2026 +0200
Fix the orders months filter when an order has no creation date (#70941)
* fix: Harden the orders months filter and image cache priming helper
Two places in core disagree with rules the performance skill states.
The orders list months filter reads the oldest and newest creation
dates with two ORDER BY ... LIMIT 1 probes. date_created_gmt is
nullable, and an ascending sort returns NULL first, so a single
non-trashed order without a creation date makes the outer MIN() fall
back to the newest date and the filter loses every earlier month. The
data store always sets a creation date, so this needs a row written
some other way (direct SQL, an import, an extension), but the plain
MIN/MAX query this replaced was not affected. Add IS NOT NULL to both
probes.
ProductUtil::prime_image_caches() unpacks the per-product gallery ID
arrays into array_merge(). The arrays keep the caller's keys, so a
collection with string keys makes PHP treat them as named arguments
and throw. No core caller passes string keys today. Re-index before
unpacking.
* test: Use minimal order fixtures in the months filter regression test
The regression test for undated orders only needs order rows with a
type, status and creation date, which is all the months filter query
reads. WC_Helper_Order::create_order() also builds a product, line
items, shipping, addresses and totals, none of which the test checks.
Switch the three fixtures to wc_create_order(). The test still fails
against trunk's query and passes with the IS NOT NULL guard.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-orders-months-filter-null-date b/plugins/woocommerce/changelog/fix-orders-months-filter-null-date
new file mode 100644
index 00000000000..e5e9f860d0e
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-orders-months-filter-null-date
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show every month in the orders list date filter when an order has no creation date.
diff --git a/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php b/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php
index aa41aef1435..86fbf90bc57 100644
--- a/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php
+++ b/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php
@@ -904,9 +904,9 @@ class ListTable extends WP_List_Table {
$wpdb->prepare(
"SELECT MIN(date_created_gmt) as min_date_gmt, MAX(date_created_gmt) as max_date_gmt
FROM (
- ( SELECT date_created_gmt FROM {$table_name} WHERE type = %s AND status != 'trash' ORDER BY date_created_gmt DESC LIMIT 1 )
+ ( SELECT date_created_gmt FROM {$table_name} WHERE type = %s AND status != 'trash' AND date_created_gmt IS NOT NULL ORDER BY date_created_gmt DESC LIMIT 1 )
UNION ALL
- ( SELECT date_created_gmt FROM {$table_name} WHERE type = %s AND status != 'trash' ORDER BY date_created_gmt ASC LIMIT 1 )
+ ( SELECT date_created_gmt FROM {$table_name} WHERE type = %s AND status != 'trash' AND date_created_gmt IS NOT NULL ORDER BY date_created_gmt ASC LIMIT 1 )
) d",
$this->order_type,
$this->order_type
diff --git a/plugins/woocommerce/src/Internal/Utilities/ProductUtil.php b/plugins/woocommerce/src/Internal/Utilities/ProductUtil.php
index 6673e0cef69..883ad51c30a 100644
--- a/plugins/woocommerce/src/Internal/Utilities/ProductUtil.php
+++ b/plugins/woocommerce/src/Internal/Utilities/ProductUtil.php
@@ -327,7 +327,7 @@ class ProductUtil {
$products = array_filter( $products, static fn( $product ) => $product instanceof \WC_Product );
$featured = array_map( static fn( $product ) => $product->get_image_id(), $products );
$gallery = array_map( static fn( $product ) => $product->get_gallery_image_ids(), $products );
- $image_ids = array_filter( array_unique( array_map( 'intval', array_merge( $featured, ...$gallery ) ) ) );
+ $image_ids = array_filter( array_unique( array_map( 'intval', array_merge( $featured, ...array_values( $gallery ) ) ) ) );
if ( ! empty( $image_ids ) ) {
// Prime caches to reduce future queries.
_prime_post_caches( $image_ids );
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
index 4c8519c7fad..b793286bc76 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
@@ -5,6 +5,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Admin\Orders;
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Internal\Admin\Orders\ListTable;
+use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\Utilities\OrderUtil;
@@ -134,6 +135,34 @@ class ListTableTest extends \WC_Unit_Test_Case {
$this->assertEquals( 2, end( $year_months )->month );
}
+ /**
+ * @testdox The oldest month is still found when an order has no creation date.
+ */
+ public function test_get_months_filter_options_ignores_null_creation_date(): void {
+ global $wpdb;
+
+ $order = wc_create_order();
+ $order->set_date_created( new \WC_DateTime( '2025-01-02 00:00:00' ) );
+ $order->save();
+
+ $order = wc_create_order();
+ $order->set_date_created( new \WC_DateTime( '2025-03-02 00:00:00' ) );
+ $order->save();
+
+ // The data store always sets a creation date, so only a direct write can leave it NULL.
+ $undated_order = wc_create_order();
+ $wpdb->update(
+ OrdersTableDataStore::get_orders_table_name(),
+ array( 'date_created_gmt' => null ),
+ array( 'id' => $undated_order->get_id() )
+ );
+
+ $year_months = $this->call_get_months_filter_options( $this->sut );
+
+ $this->assertEquals( 2025, end( $year_months )->year, 'The oldest dated order should set the first year' );
+ $this->assertEquals( 1, end( $year_months )->month, 'The oldest dated order should set the first month' );
+ }
+
/**
* @testdox The months filter options works as expected with only one month.
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php b/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
index 887b0e4c6fd..b78c29d72aa 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Utilities/ProductUtilTest.php
@@ -99,6 +99,26 @@ class ProductUtilTest extends \WC_Unit_Test_Case {
}
}
+ /**
+ * @testdox prime_image_caches accepts a collection with string keys.
+ */
+ public function test_prime_image_caches_accepts_string_keyed_collection(): void {
+ $featured_id = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
+ $gallery_id = self::factory()->post->create( array( 'post_type' => 'attachment' ) );
+ $product = \WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $featured_id );
+ $product->set_gallery_image_ids( array( $gallery_id ) );
+ $product->save();
+ clean_post_cache( $featured_id );
+ clean_post_cache( $gallery_id );
+ $sut = wc_get_container()->get( ProductUtil::class );
+
+ $sut->prime_image_caches( array( 'first' => $product ) );
+
+ $this->assertNotFalse( wp_cache_get( $featured_id, 'posts' ), 'The featured image post should be cached' );
+ $this->assertNotFalse( wp_cache_get( $gallery_id, 'posts' ), 'The gallery image post should be cached' );
+ }
+
/**
* @testdox get_product_id extracts IDs from the product references accepted by the factory.
*/