Commit 82b2cc872fd for woocommerce
commit 82b2cc872fd820dab99a3b2437e60b68efcfb021
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Wed May 13 09:13:18 2026 +0200
Fix Add to Cart + Options steppers not working on extension quantity inputs (#64819)
* Fix Add to Cart + Options steppers not working on extension quantity inputs
* Add changelog
* Update has_visible_quantity_input() as well
* Styling
* Add unit tests
---------
Co-authored-by: Panos (Panagiotis Synetos) <2484390+PanosSynetos@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/fix-add-to-cart-form-steppers-extension-inputs b/plugins/woocommerce/changelog/fix-add-to-cart-form-steppers-extension-inputs
new file mode 100644
index 00000000000..61fccb6ff6d
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-add-to-cart-form-steppers-extension-inputs
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix Add to Cart + Options steppers not working on extension quantity inputs
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartForm.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartForm.php
index 21d95528fa9..b7fb729455b 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartForm.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartForm.php
@@ -118,7 +118,7 @@ class AddToCartForm extends AbstractBlock {
if (
$processor->get_tag() === 'INPUT' &&
- $processor->get_attribute( 'name' ) === 'quantity' &&
+ $processor->has_class( 'qty' ) &&
$processor->get_attribute( 'type' ) !== 'hidden'
) {
$processor->add_class( 'wc-block-components-quantity-selector__input' );
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
index 472b7dab08b..54efc13832e 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
@@ -23,7 +23,7 @@ class Utils {
while ( $processor->next_tag() ) {
if (
$processor->get_tag() === 'INPUT' &&
- $processor->get_attribute( 'name' ) === 'quantity' &&
+ $processor->has_class( 'qty' ) &&
$processor->get_attribute( 'type' ) !== 'hidden'
) {
return true;
@@ -31,6 +31,7 @@ class Utils {
}
return false;
}
+
/**
* Add increment and decrement buttons to the quantity input field.
*
@@ -84,7 +85,7 @@ class Utils {
if (
$processor->get_tag() === 'INPUT' &&
- $processor->get_attribute( 'name' ) === 'quantity' &&
+ $processor->has_class( 'qty' ) &&
$processor->get_attribute( 'type' ) !== 'hidden'
) {
$processor->add_class( 'wc-block-components-quantity-selector__input' );
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartForm.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartForm.php
new file mode 100644
index 00000000000..208c7265fd3
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartForm.php
@@ -0,0 +1,48 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;
+
+use Automattic\WooCommerce\Blocks\Assets\Api;
+use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
+use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
+use Automattic\WooCommerce\Blocks\Package;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for the AddToCartForm block type.
+ */
+class AddToCartForm extends WC_Unit_Test_Case {
+
+ /**
+ * Tests that add_stepper_classes_to_add_to_cart_form_input adds wrapper and input classes to inputs.
+ *
+ * @covers \Automattic\WooCommerce\Blocks\BlockTypes\AddToCartForm::add_stepper_classes_to_add_to_cart_form_input
+ */
+ public function test_add_stepper_classes_to_add_to_cart_form_input(): void {
+ $quantity_html = '<div class="quantity"><input type="number" class="input-text qty text" name="custom_name" value="1" /></div>';
+
+ $block = new class(
+ Package::container()->get( Api::class ),
+ Package::container()->get( AssetDataRegistry::class ),
+ new IntegrationRegistry()
+ ) extends \Automattic\WooCommerce\Blocks\BlockTypes\AddToCartForm {
+ /**
+ * Skip block registration; woocommerce/add-to-cart-form is already registered when WooCommerce loads.
+ */
+ protected function initialize() {
+ }
+ };
+
+ $reflection = new \ReflectionClass( $block );
+ $method = $reflection->getMethod( 'add_stepper_classes_to_add_to_cart_form_input' );
+ $method->setAccessible( true );
+
+ $result = $method->invoke( $block, $quantity_html );
+
+ $this->assertStringContainsString( 'wc-block-components-quantity-selector', $result, 'The quantity wrapper should receive the stepper wrapper class.' );
+ $this->assertStringContainsString( 'wc-block-components-quantity-selector__input', $result, 'The input should receive the stepper input class.' );
+ $this->assertStringContainsString( 'custom_name', $result, 'The original input name value should be preserved.' );
+ }
+}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartWithOptions.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartWithOptions.php
index 4069dd5b4e4..79fcd36a01e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartWithOptions.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/AddToCartWithOptions.php
@@ -16,6 +16,7 @@ use Automattic\WooCommerce\Tests\Blocks\Mocks\AddToCartWithOptionsVariationSelec
use Automattic\WooCommerce\Tests\Blocks\Mocks\AddToCartWithOptionsVariationSelectorAttributeMock;
use Automattic\WooCommerce\Tests\Blocks\Mocks\AddToCartWithOptionsVariationSelectorAttributeNameMock;
use Automattic\WooCommerce\Tests\Blocks\Mocks\AddToCartWithOptionsVariationSelectorAttributeOptionsMock;
+use Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions\Utils;
/**
* Tests for the AddToCartWithOptions block type
@@ -551,4 +552,19 @@ class AddToCartWithOptions extends \WP_UnitTestCase {
remove_filter( 'woocommerce_quantity_input_args', $filter, 10 );
}
}
+
+ /**
+ * Tests that add_quantity_stepper_classes adds wrapper and input classes to inputs.
+ *
+ * @covers \Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions\Utils::add_quantity_stepper_classes
+ */
+ public function test_add_quantity_stepper_classes() {
+ $quantity_html = '<div class="quantity"><input type="number" class="input-text qty text" name="custom_name" value="1" /></div>';
+
+ $result = Utils::add_quantity_stepper_classes( $quantity_html );
+
+ $this->assertStringContainsString( 'wc-block-components-quantity-selector', $result, 'The quantity wrapper should receive the stepper wrapper class.' );
+ $this->assertStringContainsString( 'wc-block-components-quantity-selector__input', $result, 'The input should receive the stepper input class.' );
+ $this->assertStringContainsString( 'custom_name', $result, 'The original input name value should be preserved.' );
+ }
}