Commit af7497b2c56 for woocommerce
commit af7497b2c5626ae42454225e67ead8edd2db76c3
Author: Alba Rincón <albarin@users.noreply.github.com>
Date: Thu Jun 4 10:27:43 2026 +0200
Batch-prime post caches in analytics Reports include_extended_info (#65468)
* Batch-prime post caches in analytics Reports include_extended_info
* Add changefile(s) from automation for the following project(s): woocommerce
* Add changefile(s) from automation for the following project(s): woocommerce
* Add changefile(s) from automation for the following project(s): woocommerce
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/65468-performance-prime-analytics-extended-info b/plugins/woocommerce/changelog/65468-performance-prime-analytics-extended-info
new file mode 100644
index 00000000000..9f0ebfdbb68
--- /dev/null
+++ b/plugins/woocommerce/changelog/65468-performance-prime-analytics-extended-info
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Improve the performance of the analytics Products and Variations reports by batch-priming product caches.
\ No newline at end of file
diff --git a/plugins/woocommerce/src/Admin/API/Reports/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/DataStore.php
index 925a523ac46..4b5a3d3120d 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/DataStore.php
@@ -290,6 +290,27 @@ class DataStore extends SqlQuery implements DataStoreInterface {
}
}
+ /**
+ * Batch-prime post + meta caches for a list of IDs, plus their `_thumbnail_id` attachments.
+ *
+ * @param array $ids Post IDs to prime.
+ * @return void
+ */
+ protected static function prime_object_caches( array $ids ): void {
+ $ids = array_unique( array_filter( array_map( 'intval', $ids ) ) );
+ if ( empty( $ids ) ) {
+ return;
+ }
+ _prime_post_caches( $ids );
+
+ $image_ids = array_filter(
+ array_map( static fn( $id ) => (int) get_post_meta( $id, '_thumbnail_id', true ), $ids )
+ );
+ if ( ! empty( $image_ids ) ) {
+ _prime_post_caches( array_unique( $image_ids ) );
+ }
+ }
+
/**
* Whether or not the report should use the caching layer.
*
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Products/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Products/DataStore.php
index 20fa3daf468..05e7304a589 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Products/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Products/DataStore.php
@@ -250,6 +250,10 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
global $wpdb;
$product_names = array();
+ if ( $query_args['extended_info'] ) {
+ self::prime_object_caches( array_column( $products_data, 'product_id' ) );
+ }
+
foreach ( $products_data as $key => $product_data ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Variations/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Variations/DataStore.php
index daa27634008..8175f83c5d5 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Variations/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Variations/DataStore.php
@@ -242,6 +242,15 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$products_data, $query_args ) {
+ if ( $query_args['extended_info'] ) {
+ self::prime_object_caches(
+ array_merge(
+ array_column( $products_data, 'product_id' ),
+ array_column( $products_data, 'variation_id' )
+ )
+ );
+ }
+
foreach ( $products_data as $key => $product_data ) {
$extended_info = new \ArrayObject();
if ( $query_args['extended_info'] ) {