Commit 60e474ce1aa for woocommerce

commit 60e474ce1aa11e58ae25111c2368ab7a74c25a3a
Author: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>
Date:   Mon Nov 30 11:18:06 2026 +0200

    Fix stock notification lists re-reading each notification (#71546)

    * fix: Build stock notification lists from fetched rows, not per-row reads

    * refactor: Reset list notifications with set_defaults() like HPOS orders

    * refactor: Name notification row helpers after HPOS order equivalents

    * refactor: Keep notification row helpers private and drop redundant casts

diff --git a/plugins/woocommerce/changelog/71378-stock-notification-list-reads b/plugins/woocommerce/changelog/71378-stock-notification-list-reads
new file mode 100644
index 00000000000..e11ae7c28b1
--- /dev/null
+++ b/plugins/woocommerce/changelog/71378-stock-notification-list-reads
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Load stock notification lists with fewer database queries in the admin, My Account, and REST API.
diff --git a/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php b/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
index 1a797e17427..93d4d309a37 100644
--- a/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
+++ b/plugins/woocommerce/src/Internal/DataStores/StockNotifications/StockNotificationsDataStore.php
@@ -250,25 +250,51 @@ CREATE TABLE $meta_table_name (
 			throw new \Exception( 'Stock notification not found' );
 		}

+		$this->set_notification_props_from_data( $notification, (array) $data );
+
+		$notification->read_meta_data();
+		$notification->set_object_read( true );
+	}
+
+	/**
+	 * Initialize a notification object from a row of the notifications table and its meta, without reading it again.
+	 *
+	 * @param Notification $notification The data object to initialize.
+	 * @param array        $data         A row of notification data from the database.
+	 * @param array        $meta_data    The notification's raw meta data rows.
+	 * @return void
+	 */
+	private function init_notification_record( Notification $notification, array $data, array $meta_data ): void {
+		$notification->set_defaults();
+		$this->set_notification_props_from_data( $notification, $data );
+		$notification->init_meta_data( $this->filter_raw_meta_data( $notification, $meta_data ) );
+		$notification->set_object_read( true );
+	}
+
+	/**
+	 * Set a notification's props from a row of the notifications table.
+	 *
+	 * @param Notification $notification The data object to populate.
+	 * @param array        $data         A row of notification data from the database.
+	 * @return void
+	 */
+	private function set_notification_props_from_data( Notification $notification, array $data ): void {
 		$notification->set_props(
 			array(
-				'id'                  => $data->id,
-				'product_id'          => $data->product_id,
-				'user_id'             => $data->user_id,
-				'user_email'          => $data->user_email,
-				'status'              => $data->status,
-				'date_created'        => wc_string_to_timestamp( $data->date_created_gmt ),
-				'date_modified'       => wc_string_to_timestamp( $data->date_modified_gmt ),
-				'date_confirmed'      => wc_string_to_timestamp( $data->date_confirmed_gmt ),
-				'date_last_attempt'   => wc_string_to_timestamp( $data->date_last_attempt_gmt ),
-				'date_notified'       => wc_string_to_timestamp( $data->date_notified_gmt ),
-				'date_cancelled'      => wc_string_to_timestamp( $data->date_cancelled_gmt ),
-				'cancellation_source' => $data->cancellation_source,
+				'id'                  => $data['id'],
+				'product_id'          => $data['product_id'],
+				'user_id'             => $data['user_id'],
+				'user_email'          => $data['user_email'],
+				'status'              => $data['status'],
+				'date_created'        => wc_string_to_timestamp( $data['date_created_gmt'] ),
+				'date_modified'       => wc_string_to_timestamp( $data['date_modified_gmt'] ),
+				'date_confirmed'      => wc_string_to_timestamp( $data['date_confirmed_gmt'] ),
+				'date_last_attempt'   => wc_string_to_timestamp( $data['date_last_attempt_gmt'] ),
+				'date_notified'       => wc_string_to_timestamp( $data['date_notified_gmt'] ),
+				'date_cancelled'      => wc_string_to_timestamp( $data['date_cancelled_gmt'] ),
+				'cancellation_source' => $data['cancellation_source'],
 			)
 		);
-
-		$notification->read_meta_data();
-		$notification->set_object_read( true );
 	}

 	/**
@@ -586,10 +612,13 @@ CREATE TABLE $meta_table_name (
 		}

 		if ( 'objects' === $args['return'] ) {
+			$meta_data = $this->data_store_meta->get_meta_data_for_object_ids( array_column( $results, 'id' ) );

 			return array_map(
-				function ( $result ) {
-					return new Notification( $result );
+				function ( $result ) use ( $meta_data ) {
+					$notification = new Notification();
+					$this->init_notification_record( $notification, $result, $meta_data[ $result['id'] ] );
+					return $notification;
 				},
 				$results
 			);
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 dc29cb7c32c..11f528efdb1 100644
--- a/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/StockNotifications/StockNotificationsDataStoreTests.php
@@ -974,6 +974,40 @@ class StockNotificationsDataStoreTests extends \WC_Unit_Test_Case {
 		$this->assertEquals( 'test@test.com', $notifications[0]->get_user_email() );
 	}

+	/**
+	 * @testdox query() returning objects should build them from the fetched rows and load all meta in one query.
+	 */
+	public function test_query_objects_does_not_read_each_notification(): void {
+		global $wpdb;
+
+		$ids = array();
+		for ( $i = 1; $i <= 3; $i++ ) {
+			$notification = new Notification();
+			$notification->set_product_id( 1 );
+			$notification->set_user_email( "test{$i}@test.com" );
+			$notification->add_meta_data( 'test_meta', "value_{$i}" );
+			$notification->save();
+			$ids[] = $notification->get_id();
+		}
+
+		$queries_before = $wpdb->num_queries;
+		$notifications  = $this->data_store->query(
+			array(
+				'product_id' => 1,
+				'return'     => 'objects',
+			)
+		);
+
+		$this->assertSame( 2, $wpdb->num_queries - $queries_before, 'Expected one query for the rows and one for their meta.' );
+		$this->assertCount( 3, $notifications );
+		foreach ( $notifications as $index => $notification ) {
+			$this->assertSame( $ids[ $index ], $notification->get_id() );
+			$this->assertSame( 'value_' . ( $index + 1 ), $notification->get_meta( 'test_meta' ) );
+			$this->assertEquals( ( new Notification( $ids[ $index ] ) )->get_data(), $notification->get_data() );
+			$this->assertEmpty( $notification->get_changes() );
+		}
+	}
+
 	/**
 	 * @testdox notification_exists_by_email() should match a stored lowercase row from mixed-case input.
 	 */