Commit 6854cc21db for woocommerce
commit 6854cc21db4d8175e2ff1c0f9a53c6f54327971a
Author: Michael Pretty <prettyboymp@users.noreply.github.com>
Date: Tue Jan 27 07:53:06 2026 -0500
VariationSelectorAttribute: Use variation objects instead of arrays for better performance (#62795)
* VariationSelectorAttribute: Use variation objects instead of arrays for better performance
Use `get_available_variations('objects')` instead of the default 'array'
return type since only variation attributes are needed. This avoids
expensive processing (HTML generation, price calculations, image lookups)
that was being discarded.
* Update phpstan baseline
diff --git a/plugins/woocommerce/changelog/WOOPLUG-6121-variation-selector-use-objects b/plugins/woocommerce/changelog/WOOPLUG-6121-variation-selector-use-objects
new file mode 100644
index 0000000000..733a3cccdb
--- /dev/null
+++ b/plugins/woocommerce/changelog/WOOPLUG-6121-variation-selector-use-objects
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Use variation objects instead of arrays in VariationSelectorAttribute 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 e4265ac800..7741e4c300 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -57309,12 +57309,6 @@ parameters:
count: 1
path: src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelector.php
- -
- message: '#^Anonymous function has an unused use \$attribute_terms\.$#'
- identifier: closure.unusedUse
- count: 1
- path: src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
-
-
message: '#^Access to property \$context on an unknown class Automattic\\WooCommerce\\Blocks\\BlockTypes\\AddToCartWithOptions\\WP_Block\.$#'
identifier: class.notFound
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
index b67cc6b84b..ecfb1329e7 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
@@ -57,16 +57,17 @@ class VariationSelectorAttribute extends AbstractBlock {
global $product;
$attribute_terms = $this->get_terms( $attribute_name, $product_attribute_terms );
- $product_variations = $product->get_available_variations();
+ $product_variations = $product->get_available_variations( 'objects' );
// Filter out terms which are not available in any product variation.
$attribute_terms = array_filter(
$attribute_terms,
- function ( $term ) use ( $product_variations, $attribute_name, $attribute_terms ) {
- foreach ( $product_variations as $product_variation ) {
+ function ( $term ) use ( $product_variations, $attribute_name ) {
+ foreach ( $product_variations as $variation ) {
+ $attributes = $variation->get_variation_attributes();
if (
- $term['value'] === $product_variation['attributes'][ wc_variation_attribute_name( $attribute_name ) ] ||
- '' === $product_variation['attributes'][ wc_variation_attribute_name( $attribute_name ) ]
+ $term['value'] === $attributes[ wc_variation_attribute_name( $attribute_name ) ] ||
+ '' === $attributes[ wc_variation_attribute_name( $attribute_name ) ]
) {
return true;
}