Commit 27e959fe8a8 for woocommerce

commit 27e959fe8a8f5a88a8acec263c720a4f358aec43
Author: Rishabh Gupta <109821717+R1shabh-Gupta@users.noreply.github.com>
Date:   Tue Nov 24 19:31:48 2026 +0530

    Fix fatal when setting quantity for an item no longer in the cart (#71111)

    Co-authored-by: Mike Jolley <mike.jolley@me.com>
    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/71056-fix-set-quantity-unknown-cart-item b/plugins/woocommerce/changelog/71056-fix-set-quantity-unknown-cart-item
new file mode 100644
index 00000000000..3c7b0f67f06
--- /dev/null
+++ b/plugins/woocommerce/changelog/71056-fix-set-quantity-unknown-cart-item
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent a critical error when updating the quantity of an item that is no longer in the cart.
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index da8f26d45fa..75be1fd58ac 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -1577,7 +1577,7 @@ class WC_Cart extends WC_Legacy_Cart {
 	 * @param string $cart_item_key contains the id of the cart item.
 	 * @param int    $quantity contains the quantity of the item.
 	 * @param bool   $refresh_totals whether or not to calculate totals after setting the new qty. Can be used to defer calculations if setting quantities in bulk.
-	 * @return bool
+	 * @return bool  False if the item is not in the cart, true otherwise.
 	 */
 	public function set_quantity( $cart_item_key, $quantity = 1, $refresh_totals = true ) {
 		if ( 0 === $quantity || $quantity < 0 ) {
@@ -1586,6 +1586,10 @@ class WC_Cart extends WC_Legacy_Cart {
 			return $this->remove_cart_item( $cart_item_key );
 		}

+		if ( ! isset( $this->cart_contents[ $cart_item_key ] ) ) {
+			return false;
+		}
+
 		// Update qty.
 		$old_quantity                                      = $this->cart_contents[ $cart_item_key ]['quantity'];
 		$this->cart_contents[ $cart_item_key ]['quantity'] = $quantity;
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index 27fc7e01a4f..9fe1d202b9b 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -3206,6 +3206,20 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
 		$this->assertSame( 'The selected options belong to a different variation of this product.', wc_get_notices( 'error' )[0]['notice'] );
 	}

+	/**
+	 * @testdox set_quantity() returns false and leaves the cart unchanged when the item key is not in the cart.
+	 */
+	public function test_set_quantity_returns_false_for_unknown_cart_item_key(): void {
+		$product  = WC_Helper_Product::create_simple_product();
+		$item_key = WC()->cart->add_to_cart( $product->get_id(), 1 );
+
+		$result = WC()->cart->set_quantity( 'not-a-cart-item-key', 2 );
+
+		$this->assertFalse( $result, 'set_quantity() should return false for an unknown cart item key.' );
+		$this->assertSame( array( $item_key ), array_keys( WC()->cart->get_cart_contents() ), 'No cart entry should be added for an unknown key.' );
+		$this->assertSame( 1, WC()->cart->get_cart_item( $item_key )['quantity'], 'The existing item quantity should not change.' );
+	}
+
 	/**
 	 * Run the classic checkout's per-user check for a guest and return the error notices it added.
 	 *