Commit 5be5dc8aac1 for woocommerce
commit 5be5dc8aac11b79edc81215d9d211f8d0307b6a2
Author: Ján Mikláš <neosinner@gmail.com>
Date: Thu Jul 9 08:21:28 2026 +0200
Prime post caches in count_matching_variations() (#66350)
count_matching_variations() looped over get_children() calling
wc_get_product() per child without priming the post caches, so each
call could trigger its own database query on variation-specific URLs.
Prime the caches up front, mirroring the grouped-product branch in
generate_product_data().
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/wooplug-6986-prime-variation-count-caches b/plugins/woocommerce/changelog/wooplug-6986-prime-variation-count-caches
new file mode 100644
index 00000000000..6fddc81dc1f
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooplug-6986-prime-variation-count-caches
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Prime post caches in structured data variation counting to avoid a database query per child variation.
diff --git a/plugins/woocommerce/includes/class-wc-structured-data.php b/plugins/woocommerce/includes/class-wc-structured-data.php
index 38cba12d28b..f4ecf4321f2 100644
--- a/plugins/woocommerce/includes/class-wc-structured-data.php
+++ b/plugins/woocommerce/includes/class-wc-structured-data.php
@@ -830,8 +830,14 @@ class WC_Structured_Data {
*/
private function count_matching_variations( $product, $match_attributes ) {
$match_count = 0;
+ $child_ids = $product->get_children();
- foreach ( $product->get_children() as $variation_id ) {
+ if ( ! empty( $child_ids ) ) {
+ // Prime caches to reduce future queries.
+ _prime_post_caches( $child_ids );
+ }
+
+ foreach ( $child_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( ! $variation instanceof WC_Product_Variation ) {