Commit 2d5b3d8c20d for woocommerce
commit 2d5b3d8c20dc40c4b86ef7fe368214ed4023a6ee
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 18:47:49 2026 +0100
Normalize product IDs on grouped add-to-cart before validating them (#71230)
(cherry picked from commit 78ea7a4a525c547a17627cd53647e1f5bc5ea694)
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
diff --git a/plugins/woocommerce/includes/class-wc-form-handler.php b/plugins/woocommerce/includes/class-wc-form-handler.php
index 7f759c60e3b..ac91636bb0c 100644
--- a/plugins/woocommerce/includes/class-wc-form-handler.php
+++ b/plugins/woocommerce/includes/class-wc-form-handler.php
@@ -1409,8 +1409,9 @@ class WC_Form_Handler {
$quantity_set = false;
foreach ( $items as $item => $quantity ) {
+ $item = absint( $item );
$quantity = wc_stock_amount( $quantity );
- if ( $quantity <= 0 ) {
+ if ( ! $item || $quantity <= 0 ) {
continue;
}
$quantity_set = true;
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
index eeff12c0333..cd25360e1a6 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
@@ -1281,6 +1281,47 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
$this->assertSame( $expected, WC_Form_Handler::remove_add_to_cart_from_canonical_redirect( $redirect_url, $requested_url ) );
}
+ /**
+ * @testdox add_to_cart_action() validates the same product ID it adds for each item of a grouped product form.
+ *
+ * @covers WC_Form_Handler::add_to_cart_action()
+ * @testWith ["%d"]
+ * ["-%d"]
+ * ["%dabc"]
+ *
+ * @param string $key_format Format of the quantity[] key naming the product.
+ */
+ public function test_add_to_cart_action_rejects_protected_product_in_grouped_form( string $key_format ): void {
+ $grouped = WC_Helper_Product::create_grouped_product();
+ $protected = WC_Helper_Product::create_simple_product();
+ wp_update_post(
+ array(
+ 'ID' => $protected->get_id(),
+ 'post_password' => 'secret',
+ )
+ );
+ WC()->cart->empty_cart();
+
+ $validated_ids = array();
+ $validation = function ( $passed, $product_id ) use ( &$validated_ids ) {
+ $validated_ids[] = $product_id;
+ return $passed;
+ };
+ add_filter( 'woocommerce_add_to_cart_validation', $validation, 10, 2 );
+
+ $_REQUEST = array(
+ 'add-to-cart' => (string) $grouped->get_id(),
+ 'quantity' => array( sprintf( $key_format, $protected->get_id() ) => '1' ),
+ );
+ WC_Form_Handler::add_to_cart_action();
+
+ remove_filter( 'woocommerce_add_to_cart_validation', $validation, 10 );
+
+ $this->assertSame( array( $protected->get_id() ), $validated_ids, 'Validation should see the ID WC_Cart::add_to_cart() would add.' );
+ $this->assertCount( 0, WC()->cart->get_cart(), 'The protected product should not be added to the cart.' );
+ $this->assertSame( 1, wc_notice_count( 'error' ), 'The form should display the protected-product notice.' );
+ }
+
/**
* Prepares request globals for the account details handler.
*