Commit e72ffacbfcb for woocommerce

commit e72ffacbfcb1ec521261f1263e17ad1f4d866bb3
Author: Amit Raj <77401999+amitraj2203@users.noreply.github.com>
Date:   Tue May 26 14:12:34 2026 +0530

    Prime attachment post caches when listing product variations and products via REST API (#65271)

    * Prime attachment caches in WC_REST_Product_Variations_Controller::get_objects()

    Calling _prime_post_caches() after loading variation objects batches all
    variation image attachment queries into one instead of firing get_post()
    individually per variation. Reduces queries by 1 per variation with an image.

    * Prime attachment caches in v4 Products Controller::get_images()

    Calling _prime_post_caches() before iterating over a product's attachment IDs
    batches the post and meta queries into 2 regardless of how many images the
    product has. A product with 4 gallery images drops from 8 queries to 2.

    * Add changelog file

diff --git a/plugins/woocommerce/changelog/64069-prime-caches-variations-rest b/plugins/woocommerce/changelog/64069-prime-caches-variations-rest
new file mode 100644
index 00000000000..ed3baaeef7e
--- /dev/null
+++ b/plugins/woocommerce/changelog/64069-prime-caches-variations-rest
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Reduce database queries when listing product variations and products via the REST API by priming attachment post caches before iterating over images.
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-variations-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-variations-controller.php
index 3ed2b60d4d9..410c6c40e5f 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-variations-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-product-variations-controller.php
@@ -1179,6 +1179,12 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
 			$this->exclude_status = array();
 		}

+		$attachment_ids = array_filter( array_map( fn( $variation ) => (int) $variation->get_image_id(), $result['objects'] ) );
+		if ( ! empty( $attachment_ids ) ) {
+			// Prime caches to reduce future queries.
+			_prime_post_caches( $attachment_ids );
+		}
+
 		return $result;
 	}

diff --git a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
index 4a97975ffac..a851fe00518 100644
--- a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
+++ b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
@@ -290,6 +290,11 @@ class Controller extends WC_REST_Products_V2_Controller {
 		// Add gallery images.
 		$attachment_ids = array_merge( $attachment_ids, $product->get_gallery_image_ids() );

+		if ( ! empty( $attachment_ids ) ) {
+			// Prime caches to reduce future queries.
+			_prime_post_caches( $attachment_ids );
+		}
+
 		// Build image data.
 		foreach ( $attachment_ids as $attachment_id ) {
 			$attachment_post = get_post( $attachment_id );