Commit d1f1f1bdd16 for woocommerce

commit d1f1f1bdd168fbf0df8db67ec28ba01a6eea43f7
Author: Alba Rincón <albarin@users.noreply.github.com>
Date:   Thu May 28 16:01:41 2026 +0200

    Prime taxonomy thumbnail caches in REST and ProductCategories block (#65320)

    * Prime taxonomy thumbnail caches in REST and ProductCategories block

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Use foreach for consistency with REST priming

    * Compact thumbnail ID collection in taxonomy cache priming

    ---------

    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/65320-fix-64069-prime-taxonomy-thumbnails b/plugins/woocommerce/changelog/65320-fix-64069-prime-taxonomy-thumbnails
new file mode 100644
index 00000000000..43bf636e15a
--- /dev/null
+++ b/plugins/woocommerce/changelog/65320-fix-64069-prime-taxonomy-thumbnails
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Reduce database queries when listing product categories or brands via REST, and when rendering the Product Categories List block with images, by priming thumbnail attachment caches.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php
index 85e0bd9c225..5e98dbdd082 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php
@@ -345,9 +345,18 @@ abstract class WC_REST_Terms_Controller extends WC_REST_Controller {
 			}
 		}
 		$response = array();
-		foreach ( $query_result as $term ) {
-			$data       = $this->prepare_item_for_response( $term, $request );
-			$response[] = $this->prepare_response_for_collection( $data );
+		if ( is_array( $query_result ) ) {
+			$terms          = array_filter( $query_result, static fn( $term ) => $term instanceof \WP_Term );
+			$attachment_ids = array_filter( array_map( static fn( $term ) => (int) get_term_meta( $term->term_id, 'thumbnail_id', true ), $terms ) );
+			if ( ! empty( $attachment_ids ) ) {
+				// Prime caches to reduce future queries.
+				_prime_post_caches( $attachment_ids );
+			}
+
+			foreach ( $query_result as $term ) {
+				$data       = $this->prepare_item_for_response( $term, $request );
+				$response[] = $this->prepare_response_for_collection( $data );
+			}
 		}

 		$response = rest_ensure_response( $response );
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index b3164926d6e..68315b40a13 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -30981,12 +30981,6 @@ parameters:
 			count: 1
 			path: includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php

-		-
-			message: '#^Argument of an invalid type array\|WP_Error supplied for foreach, only iterables are supported\.$#'
-			identifier: foreach.nonIterable
-			count: 1
-			path: includes/rest-api/Controllers/Version3/class-wc-rest-terms-controller.php
-
 		-
 			message: '#^Call to an undefined method WP_Error\|WP_REST_Response\:\:header\(\)\.$#'
 			identifier: method.notFound
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCategories.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCategories.php
index a9ac5c1db33..b2c3e6faed0 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCategories.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCategories.php
@@ -170,6 +170,15 @@ class ProductCategories extends AbstractDynamicBlock {
 				}
 			);
 		}
+
+		if ( ! empty( $attributes['hasImage'] ) ) {
+			$attachment_ids = array_filter( array_map( static fn( $category ) => (int) get_term_meta( $category->term_id, 'thumbnail_id', true ), $categories ) );
+			if ( ! empty( $attachment_ids ) ) {
+				// Prime caches to reduce future queries.
+				_prime_post_caches( $attachment_ids );
+			}
+		}
+
 		return $hierarchical ? $this->build_category_tree( $categories, $children_only ) : $categories;
 	}