Commit c9f5ffb4eb8 for woocommerce
commit c9f5ffb4eb8b413a0730cc2fc45d3b307940aab5
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Tue Jun 23 14:27:09 2026 +0200
Fix adding to cart variable products with an 'Any' attribute from the Add to Cart + Options block when in legacy mode (#65930)
* Add changelog
* Fix adding to cart variable products with an 'Any' attribute from the Add to Cart + Options block when in legacy mode
* Simplify solution
diff --git a/plugins/woocommerce/changelog/fix-add-to-cart-with-options-any-attribute-legacy-mode b/plugins/woocommerce/changelog/fix-add-to-cart-with-options-any-attribute-legacy-mode
new file mode 100644
index 00000000000..20444249799
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-add-to-cart-with-options-any-attribute-legacy-mode
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix adding to cart variable products with an 'Any' attribute from the Add to Cart + Options block when in legacy mode
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
index d1c9231f9ec..2f535de93f8 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/VariationSelectorAttribute.php
@@ -132,11 +132,20 @@ class VariationSelectorAttribute extends AbstractBlock {
'data-wp-init' => 'callbacks.setDefaultSelectedAttribute',
);
+ // Hidden input for legacy form POST submissions (page refresh). Chips and
+ // dropdown UI elements do not include name="attribute_*" fields.
+ $hidden_attribute_input = sprintf(
+ '<input type="hidden" name="%1$s" value="%2$s" data-wp-bind--value="context.selectedValue" />',
+ esc_attr( $attribute_slug ),
+ esc_attr( $default_selected ?? '' )
+ );
+
return sprintf(
- '<div %s %s>%s</div>',
+ '<div %s %s>%s%s</div>',
get_block_wrapper_attributes( $interactive_attributes ),
wp_interactivity_data_wp_context( $interactive_context ),
- $inner_html
+ $inner_html,
+ $hidden_attribute_input
);
}
diff --git a/plugins/woocommerce/tests/e2e/tests/blocks/add-to-cart-with-options/add-to-cart-with-options.block_theme.spec.ts b/plugins/woocommerce/tests/e2e/tests/blocks/add-to-cart-with-options/add-to-cart-with-options.block_theme.spec.ts
index e8ac0c326cc..52b87431ed3 100644
--- a/plugins/woocommerce/tests/e2e/tests/blocks/add-to-cart-with-options/add-to-cart-with-options.block_theme.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/blocks/add-to-cart-with-options/add-to-cart-with-options.block_theme.spec.ts
@@ -1226,7 +1226,9 @@ test.describe( 'Add to Cart + Options Block', () => {
isOnlyCurrentEntityDirty: true,
} );
- await page.goto( '/product/hoodie' );
+ // We intentionally test the V-Neck T-Shirt because it has variations
+ // using 'any' as a variation attribute.
+ await page.goto( '/product/v-neck-t-shirt/' );
const addToCartBlock = page.locator(
'.wp-block-add-to-cart-with-options'
@@ -1234,12 +1236,12 @@ test.describe( 'Add to Cart + Options Block', () => {
const colorBlueOption = addToCartBlock
.getByRole( 'radiogroup', { name: 'Color' } )
.getByRole( 'radio', { name: 'Blue', exact: true } );
- const logoYesOption = addToCartBlock
- .getByRole( 'radiogroup', { name: 'Logo' } )
- .getByRole( 'radio', { name: 'Yes', exact: true } );
+ const sizeLargeOption = addToCartBlock
+ .getByRole( 'radiogroup', { name: 'Size' } )
+ .getByRole( 'radio', { name: 'Large', exact: true } );
await colorBlueOption.click();
- await logoYesOption.click();
+ await sizeLargeOption.click();
const addToCartButton = page.getByRole( 'button', {
name: 'Add to cart',
@@ -1248,7 +1250,7 @@ test.describe( 'Add to Cart + Options Block', () => {
await addToCartButton.click();
await expect(
- page.getByLabel( 'Quantity of Hoodie in your cart.' )
+ page.getByLabel( 'Quantity of V-Neck T-Shirt in your cart.' )
).toHaveValue( '1' );
} );