Commit ffc661ec19 for woocommerce
commit ffc661ec196a59f8e42d8625e66868a52602b74a
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Mon Feb 9 08:34:14 2026 +0100
Only disable add-to-cart button in variable products when wc-add-to-cart-variation is enqueued (#63171)
* Only disable add-to-cart button in variable products when wc-add-to-cart-variation is enqueued
* Add changelog file
* Update template version
* Simplify logic
diff --git a/plugins/woocommerce/changelog/fix-wc-10.5-variable-add-to-cart-button-disabled b/plugins/woocommerce/changelog/fix-wc-10.5-variable-add-to-cart-button-disabled
new file mode 100644
index 0000000000..8873b1ea7d
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-wc-10.5-variable-add-to-cart-button-disabled
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Only disable add-to-cart button in variable products when wc-add-to-cart-variation is enqueued
diff --git a/plugins/woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php b/plugins/woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php
index 6161e1104f..c6f115576a 100644
--- a/plugins/woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php
+++ b/plugins/woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php
@@ -4,12 +4,20 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
- * @version 10.5.0
+ * @version 10.5.1
*/
defined( 'ABSPATH' ) || exit;
global $product;
+
+/*
+ * By default, the add to cart button is disabled to prevent shoppers from interacting with it
+ * while the WooCommerce variation script is still loading. If the default variation script
+ * (wc-add-to-cart-variation) is not enqueued, the button remains enabled to ensure compatibility
+ * with stores that use this template without the script.
+ */
+$is_add_to_cart_button_disabled = wp_script_is( 'wc-add-to-cart-variation', 'enqueued' );
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
@@ -28,7 +36,7 @@ global $product;
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
- <button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" disabled><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
+ <button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"<?php echo $is_add_to_cart_button_disabled ? ' disabled' : ''; ?>><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-variation-add-to-cart-button-test.php b/plugins/woocommerce/tests/php/includes/class-wc-variation-add-to-cart-button-test.php
new file mode 100644
index 0000000000..f0b446c612
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/class-wc-variation-add-to-cart-button-test.php
@@ -0,0 +1,103 @@
+<?php
+declare( strict_types = 1 );
+
+/**
+ * Unit tests for the variation add-to-cart button template and disabled state.
+ *
+ * @package WooCommerce\Tests\Includes
+ */
+
+/**
+ * Class WC_Variation_Add_To_Cart_Button_Test
+ */
+class WC_Variation_Add_To_Cart_Button_Test extends \WC_Unit_Test_Case {
+
+ /**
+ * Variable product for template tests.
+ *
+ * @var WC_Product_Variable
+ */
+ private $product;
+
+ /**
+ * Runs before each test.
+ */
+ public function setUp(): void {
+ parent::setUp();
+ $this->product = WC_Helper_Product::create_variation_product();
+ }
+
+ /**
+ * Runs after each test.
+ */
+ public function tearDown(): void {
+ global $product;
+ $product = null;
+ wp_dequeue_script( 'wc-add-to-cart-variation' );
+ parent::tearDown();
+ }
+
+ /**
+ * Renders the variation add-to-cart button template and returns HTML.
+ *
+ * @return string
+ */
+ private function render_template(): string {
+ global $product;
+ $product = $this->product;
+ return wc_get_template_html( 'single-product/add-to-cart/variation-add-to-cart-button.php' );
+ }
+
+ /**
+ * Asserts the rendered button has the disabled attribute.
+ *
+ * @param string $html Rendered template HTML.
+ */
+ private function assert_button_is_disabled( string $html ): void {
+ $this->assertStringContainsString( 'single_add_to_cart_button', $html );
+ $this->assertStringContainsString( ' disabled>', $html, 'Add to cart button should be disabled' );
+ }
+
+ /**
+ * Asserts the rendered button does not have the disabled attribute.
+ *
+ * @param string $html Rendered template HTML.
+ */
+ private function assert_button_is_enabled( string $html ): void {
+ $this->assertStringContainsString( 'single_add_to_cart_button', $html );
+ $this->assertStringNotContainsString( ' disabled>', $html, 'Add to cart button should not be disabled' );
+ }
+
+ /**
+ * Registers and enqueues the wc-add-to-cart-variation script so wp_script_is( 'wc-add-to-cart-variation', 'enqueued' ) is true.
+ */
+ private function register_and_enqueue_variation_script(): void {
+ if ( ! wp_script_is( 'wc-add-to-cart-variation', 'registered' ) ) {
+ wp_register_script(
+ 'wc-add-to-cart-variation',
+ 'https://example.com/wc-add-to-cart-variation.js',
+ array( 'jquery' ),
+ WC_VERSION,
+ true
+ );
+ }
+ wp_enqueue_script( 'wc-add-to-cart-variation' );
+ }
+
+ /**
+ * @testdox When wc-add-to-cart-variation is not enqueued, the button is enabled.
+ */
+ public function test_when_script_not_enqueued_renders_enabled_button(): void {
+ $html = $this->render_template();
+ $this->assert_button_is_enabled( $html );
+ }
+
+ /**
+ * @testdox When wc-add-to-cart-variation is enqueued, the button is disabled.
+ */
+ public function test_when_script_enqueued_renders_disabled_button(): void {
+ $this->register_and_enqueue_variation_script();
+ $html = $this->render_template();
+ $this->assert_button_is_disabled( $html );
+ }
+}