Commit 90e826ec44 for woocommerce
commit 90e826ec44c9c2737389389bf9e00510b6a3f0c5
Author: Alba Rincón <albarin@users.noreply.github.com>
Date: Fri Feb 20 16:40:19 2026 +0100
Extract `get_primed_visible_children()` helper in `WC_Product_Grouped` (#63284)
* Extract get_primed_visible_children() helper in WC_Product_Grouped
* Add changefile(s) from automation for the following project(s): woocommerce
* Update PHPStan baseline for get_primed_visible_children() refactor
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/63284-refactor-grouped-product-extract-primed-children-helper b/plugins/woocommerce/changelog/63284-refactor-grouped-product-extract-primed-children-helper
new file mode 100644
index 0000000000..c943e60b0d
--- /dev/null
+++ b/plugins/woocommerce/changelog/63284-refactor-grouped-product-extract-primed-children-helper
@@ -0,0 +1,4 @@
+Significance: minor
+Type: update
+
+Extract "get_primed_visible_children()" helper in "WC_Product_Grouped".
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/class-wc-product-grouped.php b/plugins/woocommerce/includes/class-wc-product-grouped.php
index 254637d43e..23706bb1f0 100644
--- a/plugins/woocommerce/includes/class-wc-product-grouped.php
+++ b/plugins/woocommerce/includes/class-wc-product-grouped.php
@@ -62,9 +62,7 @@ class WC_Product_Grouped extends WC_Product {
* @return bool
*/
public function is_on_sale( $context = 'view' ) {
- $child_ids = $this->get_children( $context );
- _prime_post_caches( $child_ids );
- $children = array_filter( array_map( 'wc_get_product', $child_ids ), 'wc_products_array_filter_visible_grouped' );
+ $children = $this->get_primed_visible_children( $context );
$on_sale = false;
foreach ( $children as $child ) {
@@ -95,7 +93,7 @@ class WC_Product_Grouped extends WC_Product {
public function get_price_html( $price = '' ) {
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$child_prices = array();
- $children = array_filter( array_map( 'wc_get_product', $this->get_children() ), 'wc_products_array_filter_visible_grouped' );
+ $children = $this->get_primed_visible_children();
foreach ( $children as $child ) {
if ( '' !== $child->get_price() ) {
@@ -157,11 +155,7 @@ class WC_Product_Grouped extends WC_Product {
* @return WC_Product[] Child products
*/
public function get_visible_children() {
- $grouped_products = array_map( 'wc_get_product', $this->get_children() );
- $grouped_products = array_filter( $grouped_products, 'wc_products_array_filter_visible_grouped' );
- /** @var WC_Product[] $grouped_products */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
-
- return $grouped_products;
+ return $this->get_primed_visible_children();
}
/**
@@ -171,7 +165,7 @@ class WC_Product_Grouped extends WC_Product {
* @return string Minimum price or empty string if no children
*/
public function get_min_price() {
- $children = array_filter( array_map( 'wc_get_product', $this->get_children() ), 'wc_products_array_filter_visible_grouped' );
+ $children = $this->get_primed_visible_children();
$prices = array_map( 'wc_get_price_to_display', $children );
if ( empty( $prices ) ) {
@@ -188,7 +182,7 @@ class WC_Product_Grouped extends WC_Product {
* @return string Maximum price or empty string if no children
*/
public function get_max_price() {
- $children = array_filter( array_map( 'wc_get_product', $this->get_children() ), 'wc_products_array_filter_visible_grouped' );
+ $children = $this->get_primed_visible_children();
$prices = array_map( 'wc_get_price_to_display', $children );
if ( empty( $prices ) ) {
@@ -198,6 +192,20 @@ class WC_Product_Grouped extends WC_Product {
return wc_format_decimal( max( $prices ) );
}
+ /**
+ * Get visible child products with post caches primed to avoid extra queries.
+ *
+ * @param string $context What the value is for. Valid values are view and edit.
+ * @return WC_Product[] Visible child products.
+ */
+ private function get_primed_visible_children( $context = 'view' ) {
+ $child_ids = $this->get_children( $context );
+ _prime_post_caches( $child_ids );
+ $children = array_filter( array_map( 'wc_get_product', $child_ids ), 'wc_products_array_filter_visible_grouped' );
+ /** @var WC_Product[] $children */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
+ return $children;
+ }
+
/*
|--------------------------------------------------------------------------
| Setters
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 36eb51e668..81fbfe9a0d 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -14064,30 +14064,6 @@ parameters:
count: 1
path: includes/class-wc-product-grouped.php
- -
- message: '#^Cannot call method get_price\(\) on WC_Product\|false\|null\.$#'
- identifier: method.nonObject
- count: 1
- path: includes/class-wc-product-grouped.php
-
- -
- message: '#^Cannot call method has_child\(\) on WC_Product\|false\|null\.$#'
- identifier: method.nonObject
- count: 1
- path: includes/class-wc-product-grouped.php
-
- -
- message: '#^Cannot call method is_on_sale\(\) on WC_Product\|false\|null\.$#'
- identifier: method.nonObject
- count: 1
- path: includes/class-wc-product-grouped.php
-
- -
- message: '#^Cannot call method is_purchasable\(\) on WC_Product\|false\|null\.$#'
- identifier: method.nonObject
- count: 1
- path: includes/class-wc-product-grouped.php
-
-
message: '#^Method WC_Product_Grouped\:\:set_children\(\) has no return type specified\.$#'
identifier: missingType.return
@@ -14100,12 +14076,6 @@ parameters:
count: 1
path: includes/class-wc-product-grouped.php
- -
- message: '#^Parameter \#1 \$callback of function array_map expects \(callable\(WC_Product\|false\|null\)\: mixed\)\|null, ''wc_get_price_to…'' given\.$#'
- identifier: argument.type
- count: 2
- path: includes/class-wc-product-grouped.php
-
-
message: '#^Parameter \#1 \$from of function wc_format_price_range expects string, float\|string given\.$#'
identifier: argument.type
@@ -14130,22 +14100,10 @@ parameters:
count: 1
path: includes/class-wc-product-grouped.php
- -
- message: '#^Parameter \#1 \$product of function wc_get_price_excluding_tax expects WC_Product, WC_Product\|false\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/class-wc-product-grouped.php
-
- -
- message: '#^Parameter \#1 \$product of function wc_get_price_including_tax expects WC_Product, WC_Product\|false\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/class-wc-product-grouped.php
-
-
message: '#^Parameter \#2 \$callback of function array_filter expects \(callable\(WC_Product\|false\|null\)\: bool\)\|null, ''wc_products_array…'' given\.$#'
identifier: argument.type
- count: 5
+ count: 1
path: includes/class-wc-product-grouped.php
-