Commit 5ba893c270d for woocommerce
commit 5ba893c270d79e76e9e292df8f97fe39851bbe7c
Author: Alba Rincón <albarin@users.noreply.github.com>
Date: Thu Mar 19 12:50:04 2026 +0100
Add cache priming to product grid blocks, product Button, and Product Gallery (#63750)
* Add cache priming to product grid blocks and Product Gallery
* Add changefile(s) from automation for the following project(s): woocommerce
* Add cache priming to ProductButton for grouped product children
* Add changefile(s) from automation for the following project(s): woocommerce
* Add changefile(s) from automation for the following project(s): woocommerce
* Add clarifying comment about cache priming in GroupedProductItem
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/63750-add-cache-priming-blocks b/plugins/woocommerce/changelog/63750-add-cache-priming-blocks
new file mode 100644
index 00000000000..e376fc966c4
--- /dev/null
+++ b/plugins/woocommerce/changelog/63750-add-cache-priming-blocks
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Reduce SQL queries on legacy product grid blocks, Product Gallery, and Product Button by adding missing "_prime_post_caches()" calls to batch-load post data.
\ No newline at end of file
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AbstractProductGrid.php b/plugins/woocommerce/src/Blocks/BlockTypes/AbstractProductGrid.php
index 8c809178943..7f2186d6b58 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AbstractProductGrid.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AbstractProductGrid.php
@@ -83,6 +83,9 @@ abstract class AbstractProductGrid extends AbstractDynamicBlock {
return '';
}
+ // Prime caches to reduce future queries.
+ _prime_post_caches( array_filter( array_map( fn( $product ) => (int) $product->get_image_id(), $products ) ) );
+
/**
* Override product description to prevent infinite loop.
*
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/GroupedProductItem.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/GroupedProductItem.php
index d298d84e82b..bed63e5daf3 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/GroupedProductItem.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/GroupedProductItem.php
@@ -95,6 +95,7 @@ class GroupedProductItem extends AbstractBlock {
$content = '';
+ // No need to prime post caches here, children are already cached at this point.
$children = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
foreach ( $children as $child ) {
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
index ad4a367ecbd..11692fd9300 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
@@ -339,6 +339,9 @@ class ProductButton extends AbstractBlock {
private function is_product_purchasable( $product ) {
if ( $product->is_type( ProductType::GROUPED ) ) {
$grouped_product_ids = $product->get_children();
+ if ( ! empty( $grouped_product_ids ) ) {
+ _prime_post_caches( $grouped_product_ids );
+ }
foreach ( $grouped_product_ids as $child ) {
$child_product = wc_get_product( $child );
if ( ! $child_product instanceof \WC_Product ) {
diff --git a/plugins/woocommerce/src/Blocks/Utils/ProductGalleryUtils.php b/plugins/woocommerce/src/Blocks/Utils/ProductGalleryUtils.php
index ea3ffb8cd95..4bc75c0dfce 100644
--- a/plugins/woocommerce/src/Blocks/Utils/ProductGalleryUtils.php
+++ b/plugins/woocommerce/src/Blocks/Utils/ProductGalleryUtils.php
@@ -118,6 +118,9 @@ class ProductGalleryUtils {
try {
if ( $product->is_type( 'variable' ) ) {
$variations = $product->get_children();
+ if ( ! empty( $variations ) ) {
+ _prime_post_caches( $variations );
+ }
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( $variation ) {