Commit 6fc1d4be77c for woocommerce
commit 6fc1d4be77cf286ceab3c73fa51edb83dd744e98
Author: Alba Rincón <albarin@users.noreply.github.com>
Date: Mon Apr 13 10:45:53 2026 +0200
Prime attachment caches in ProductSchema `get_images()` (#64100)
* Prime attachment caches in StoreApi ProductSchema get_images()
* Add changefile(s) from automation for the following project(s): woocommerce
* Wrap array_merge with array_filter for consistency
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/64100-fix-prime-storeapi-product-images b/plugins/woocommerce/changelog/64100-fix-prime-storeapi-product-images
new file mode 100644
index 00000000000..1221aa11bc1
--- /dev/null
+++ b/plugins/woocommerce/changelog/64100-fix-prime-storeapi-product-images
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Reduce the number of database queries on the single product page by priming attachment caches in the StoreApi ProductSchema `get_images()` method.
\ No newline at end of file
diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
index 53902d82433..46d4da98218 100644
--- a/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
+++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
@@ -659,7 +659,11 @@ class ProductSchema extends AbstractSchema {
* @return array
*/
protected function get_images( \WC_Product $product ) {
- $attachment_ids = array_merge( [ $product->get_image_id() ], $product->get_gallery_image_ids() );
+ $attachment_ids = array_filter( array_merge( [ $product->get_image_id() ], $product->get_gallery_image_ids() ) );
+ if ( ! empty( $attachment_ids ) ) {
+ // Prime caches to reduce future queries.
+ _prime_post_caches( $attachment_ids );
+ }
return array_values( array_filter( array_map( [ $this->image_attachment_schema, 'get_item_response' ], $attachment_ids ) ) );
}