Commit 31ef4a8a06 for woocommerce
commit 31ef4a8a065de1f06813ef89064efe878da814a3
Author: Michael Pretty <prettyboymp@users.noreply.github.com>
Date: Wed Jan 14 10:00:01 2026 -0500
Use variation objects instead of arrays in ProductPrice block (#62797)
* Use variation objects instead of arrays in ProductPrice block
diff --git a/plugins/woocommerce/changelog/WOOPLUG-6122-product-price-use-variation-objects b/plugins/woocommerce/changelog/WOOPLUG-6122-product-price-use-variation-objects
new file mode 100644
index 0000000000..8580b73512
--- /dev/null
+++ b/plugins/woocommerce/changelog/WOOPLUG-6122-product-price-use-variation-objects
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Use variation objects instead of arrays in ProductPrice block to avoid unnecessary data processing
\ No newline at end of file
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 066199049b..752dc88eb4 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -60957,12 +60957,6 @@ parameters:
count: 4
path: src/Blocks/BlockTypes/ProductPrice.php
- -
- message: '#^Call to an undefined method WC_Product\:\:get_available_variations\(\)\.$#'
- identifier: method.notFound
- count: 1
- path: src/Blocks/BlockTypes/ProductPrice.php
-
-
message: '#^Method Automattic\\WooCommerce\\Blocks\\BlockTypes\\ProductPrice\:\:get_block_type_uses_context\(\) has no return type specified\.$#'
identifier: missingType.return
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductPrice.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductPrice.php
index 5306deccb3..a12911b5a8 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductPrice.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductPrice.php
@@ -79,26 +79,46 @@ class ProductPrice extends AbstractBlock {
$context_directive = '';
if ( $is_interactive ) {
- $variations_data = $product->get_available_variations();
+ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- Type hint for PHPStan.
+ /** @var \WC_Product_Variable $product */
+ // Check if variation prices differ (replicates logic from WC_Product_Variable::get_available_variation).
+ $prices_vary = $product->get_variation_sale_price( 'min' ) !== $product->get_variation_sale_price( 'max' )
+ || $product->get_variation_regular_price( 'min' ) !== $product->get_variation_regular_price( 'max' );
+
$formatted_variations_data = array();
- $has_variation_price_html = false;
- foreach ( $variations_data as $variation ) {
- if (
- empty( $variation['variation_id'] )
- || ! array_key_exists( 'price_html', $variation )
- || '' === $variation['price_html']
- ) {
- continue;
+
+ if ( $prices_vary ) {
+ $variations_data = $product->get_available_variations( 'objects' );
+
+ foreach ( $variations_data as $variation ) {
+ /**
+ * Filter whether to show variation price.
+ * Replicates the filter from WC_Product_Variable::get_available_variation().
+ *
+ * @since 2.4.0
+ *
+ * @param bool $show_price Whether to show the price.
+ * @param \WC_Product_Variable $product The variable product.
+ * @param \WC_Product_Variation $variation The variation.
+ */
+ $show_variation_price = apply_filters(
+ 'woocommerce_show_variation_price',
+ true,
+ $product,
+ $variation
+ );
+
+ if ( ! $show_variation_price ) {
+ continue;
+ }
+
+ $formatted_variations_data[ $variation->get_id() ] = array(
+ 'price_html' => '<span class="price">' . $variation->get_price_html() . '</span>',
+ );
}
- // Core behavior: when all variation prices are identical, Core returns '' for variation['price_html'].
- // Therefore, the presence of any non-empty price_html implies price differences and warrants interactivity.
- $has_variation_price_html = true;
- $formatted_variations_data[ $variation['variation_id'] ] = array(
- 'price_html' => $variation['price_html'],
- );
}
- if ( ! $has_variation_price_html ) {
+ if ( empty( $formatted_variations_data ) ) {
$is_interactive = false;
} else {
wp_interactivity_config(