Commit a3199929ea2 for woocommerce

commit a3199929ea25a2a45f561e1694e6c2158420357f
Author: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>
Date:   Tue Oct 6 16:22:40 2026 +0300

    Fix stock notification counts failing under ONLY_FULL_GROUP_BY (#69274)

    fix: skip ORDER BY on stock notification counts for ONLY_FULL_GROUP_BY

diff --git a/plugins/woocommerce/changelog/wooairr-256-stock-notifications-count-order-by b/plugins/woocommerce/changelog/wooairr-256-stock-notifications-count-order-by
new file mode 100644
index 00000000000..64e48472d56
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooairr-256-stock-notifications-count-order-by
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show customers their back-in-stock sign-ups on My Account when the database enforces ONLY_FULL_GROUP_BY.
diff --git a/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php b/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
index de0da1bcfba..f9f1928ba01 100644
--- a/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
+++ b/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
@@ -485,7 +485,8 @@ CREATE TABLE $meta_table_name (
 		$order_by         = '';
 		$order_by_clauses = array();

-		if ( $args['order_by'] && is_array( $args['order_by'] ) ) {
+		// A COUNT() has no rows to sort, and ORDER BY on it fails under ONLY_FULL_GROUP_BY.
+		if ( 'count' !== $args['return'] && $args['order_by'] && is_array( $args['order_by'] ) ) {
 			foreach ( $args['order_by'] as $what => $how ) {
 				$order_by_clauses[] = $table . '.' . esc_sql( strval( $what ) ) . ' ' . esc_sql( strval( $how ) );
 			}
diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php
index 8348251da7b..3c9ad028fc7 100644
--- a/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php
@@ -565,6 +565,39 @@ class StockNotificationsDataStoreTests extends \WC_Unit_Test_Case {
 		$this->assertEquals( 2, $count );
 	}

+	/**
+	 * @testdox Should count notifications when the database runs with ONLY_FULL_GROUP_BY.
+	 */
+	public function test_query_notifications_with_return_type_count_under_only_full_group_by(): void {
+		global $wpdb;
+
+		$notification = new Notification();
+		$notification->set_product_id( 1 );
+		$notification->set_user_id( 1 );
+		$notification->save();
+
+		$notification_2 = new Notification();
+		$notification_2->set_product_id( 1 );
+		$notification_2->set_user_id( 2 );
+		$notification_2->save();
+
+		$original_sql_mode = $wpdb->get_var( 'SELECT @@SESSION.sql_mode' );
+		$wpdb->query( "SET SESSION sql_mode = 'ONLY_FULL_GROUP_BY'" );
+
+		try {
+			$count = $this->data_store->query(
+				array(
+					'return'   => 'count',
+					'order_by' => array( 'id' => 'DESC' ),
+				)
+			);
+		} finally {
+			$wpdb->query( $wpdb->prepare( 'SET SESSION sql_mode = %s', $original_sql_mode ) );
+		}
+
+		$this->assertSame( 2, $count, 'The count should ignore order_by instead of failing the query' );
+	}
+
 	/**
 	 * Test querying notifications with a return type of ids.
 	 */