Commit 9cac47c3434 for woocommerce

commit 9cac47c3434bf80e68bf0bd6fc0d82560e648a8f
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 21:29:33 2026 +0100

    Reject forbidden checkout before processing hooks (#71266)

    Forward-port the corresponding WooCommerce 11.3.1 fix to trunk.

diff --git a/plugins/woocommerce/includes/class-wc-checkout.php b/plugins/woocommerce/includes/class-wc-checkout.php
index 94c1d489d4b..5e6016ac09e 100644
--- a/plugins/woocommerce/includes/class-wc-checkout.php
+++ b/plugins/woocommerce/includes/class-wc-checkout.php
@@ -1892,6 +1892,17 @@ class WC_Checkout {

 			do_action( 'woocommerce_before_checkout_process' );

+			// Runs after the attempt hook, which extensions use to detect checkout requests, and before any processing or session work.
+			if ( ! is_user_logged_in() && ! $this->is_registration_enabled() && $this->is_registration_required() ) {
+				/**
+				 * This filter is documented in the Store API checkout route.
+				 *
+				 * @since 9.4.3
+				 */
+				$message = apply_filters( 'woocommerce_checkout_must_be_logged_in_message', __( 'You must be logged in to checkout.', 'woocommerce' ) );
+				throw new Exception( esc_html( is_string( $message ) ? $message : __( 'You must be logged in to checkout.', 'woocommerce' ) ) );
+			}
+
 			if ( WC()->cart->is_empty() ) {
 				throw new Exception( $expiry_message );
 			}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php b/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
index 551ddaeafbe..4dca3f25c08 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
@@ -229,6 +229,130 @@ class WC_Checkout_Test extends \WC_Unit_Test_Case {
 		add_filter( 'woocommerce_checkout_fields', $filter );
 	}

+	/**
+	 * @testdox Classic checkout enforces the merchant's account policy before creating a customer, order or payment.
+	 *
+	 * @testWith ["no", "no", false, false]
+	 *           ["no", "yes", false, true]
+	 *           ["yes", "no", false, true]
+	 *           ["yes", "yes", false, true]
+	 *           ["no", "no", true, true]
+	 *           ["no", "no", false, true, true, null]
+	 *           ["no", "no", false, true, null, false]
+	 *           ["yes", "no", false, false, false, null]
+	 *           ["no", "yes", false, false, null, true]
+	 *
+	 * @param string    $registration_enabled Whether checkout registration is enabled.
+	 * @param string    $guest_checkout_enabled Whether guest checkout is enabled.
+	 * @param bool      $logged_in Whether the customer is signed in.
+	 * @param bool      $allowed Whether checkout can reach customer, order and payment processing.
+	 * @param bool|null $filtered_registration_enabled Optional extension override for registration.
+	 * @param bool|null $filtered_registration_required Optional extension override for requiring registration.
+	 */
+	public function test_process_checkout_registration_policy( string $registration_enabled, string $guest_checkout_enabled, bool $logged_in, bool $allowed, ?bool $filtered_registration_enabled = null, ?bool $filtered_registration_required = null ): void {
+		remove_filter( 'woocommerce_checkout_registration_enabled', '__return_true' );
+		update_option( 'woocommerce_enable_signup_and_login_from_checkout', $registration_enabled );
+		update_option( 'woocommerce_enable_guest_checkout', $guest_checkout_enabled );
+		if ( null !== $filtered_registration_enabled ) {
+			add_filter( 'woocommerce_checkout_registration_enabled', $filtered_registration_enabled ? '__return_true' : '__return_false' );
+		}
+		if ( null !== $filtered_registration_required ) {
+			add_filter( 'woocommerce_checkout_registration_required', $filtered_registration_required ? '__return_true' : '__return_false' );
+		}
+		wp_set_current_user( $logged_in ? self::factory()->user->create( array( 'role' => 'customer' ) ) : 0 );
+		wc_clear_notices();
+
+		$product = WC_Helper_Product::create_simple_product();
+		WC()->cart->add_to_cart( $product->get_id() );
+		$order = WC_Helper_Order::create_order();
+		$order->set_total( 10 );
+		$order->save();
+		$posted_data = array( 'payment_method' => 'test_gateway' );
+
+		// Isolate this checkout policy from address validation and payment-provider side effects.
+		$sut = $this->getMockBuilder( WC_Checkout::class )->onlyMethods(
+			array( 'get_posted_data', 'update_session', 'validate_checkout', 'process_customer', 'create_order', 'process_order_payment', 'process_order_without_payment', 'send_ajax_failure_response' )
+		)->getMock();
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'get_posted_data' )->willReturn( $posted_data );
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'update_session' );
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'validate_checkout' );
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'process_customer' )->with( $posted_data );
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'create_order' )->with( $posted_data )->willReturn( $order->get_id() );
+		$sut->expects( $this->exactly( $allowed ? 1 : 0 ) )->method( 'process_order_payment' )->with( $order->get_id(), 'test_gateway' );
+		$sut->expects( $this->never() )->method( 'process_order_without_payment' );
+
+		$before_process   = did_action( 'woocommerce_before_checkout_process' );
+		$process          = did_action( 'woocommerce_checkout_process' );
+		$original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Preserve the test request.
+		$_REQUEST         = array( 'woocommerce-process-checkout-nonce' => wp_create_nonce( 'woocommerce-process_checkout' ) );
+		try {
+			$sut->process_checkout();
+		} finally {
+			$_REQUEST = $original_request;
+		}
+
+		$this->assertSame( $before_process + 1, did_action( 'woocommerce_before_checkout_process' ), 'Extensions must still see the checkout attempt.' );
+		$this->assertSame( $process + ( $allowed ? 1 : 0 ), did_action( 'woocommerce_checkout_process' ), 'Denied checkout must not dispatch processing hooks.' );
+		$this->assertSame( $allowed ? 0 : 1, wc_notice_count( 'error' ), 'Only a guest who cannot register or use guest checkout should be denied.' );
+		if ( ! $allowed ) {
+			$this->assertTrue( wc_has_notice( 'You must be logged in to checkout.', 'error' ), 'The customer should receive the account-policy error.' );
+		}
+	}
+
+	/**
+	 * @testdox Empty-cart requests fire the attempt hook, and forbidden ones get the account-policy error.
+	 * @testWith ["yes", true]
+	 *           ["no", false]
+	 * @param string $guest_checkout Whether guest checkout is enabled.
+	 * @param bool   $allowed Whether the account policy allows this request.
+	 */
+	public function test_empty_cart_preparation_hook_policy( string $guest_checkout, bool $allowed ): void {
+		remove_filter( 'woocommerce_checkout_registration_enabled', '__return_true' );
+		update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' );
+		update_option( 'woocommerce_enable_guest_checkout', $guest_checkout );
+		wp_set_current_user( 0 );
+		wc_clear_notices();
+		WC()->cart->empty_cart();
+		$sut = $this->getMockBuilder( WC_Checkout::class )->onlyMethods( array( 'get_posted_data', 'send_ajax_failure_response' ) )->getMock();
+		$sut->expects( $this->never() )->method( 'get_posted_data' );
+		$before_process   = did_action( 'woocommerce_before_checkout_process' );
+		$original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Preserve the test request.
+		$_REQUEST         = array( 'woocommerce-process-checkout-nonce' => wp_create_nonce( 'woocommerce-process_checkout' ) );
+		try {
+			$sut->process_checkout();
+		} finally {
+			$_REQUEST = $original_request;
+		}
+		$this->assertSame( $before_process + 1, did_action( 'woocommerce_before_checkout_process' ) );
+		$this->assertSame( ! $allowed, wc_has_notice( 'You must be logged in to checkout.', 'error' ) );
+	}
+
+	/**
+	 * @testdox Posted createaccount cannot enable registration when the merchant has disabled it.
+	 *
+	 * @testWith ["no", 0]
+	 *           ["yes", 1]
+	 *
+	 * @param string $registration_enabled Whether checkout registration is enabled.
+	 * @param int    $expected_createaccount Expected sanitized account-creation choice.
+	 */
+	public function test_get_posted_data_honors_registration_setting( string $registration_enabled, int $expected_createaccount ): void {
+		remove_filter( 'woocommerce_checkout_registration_enabled', '__return_true' );
+		update_option( 'woocommerce_enable_signup_and_login_from_checkout', $registration_enabled );
+		update_option( 'woocommerce_enable_guest_checkout', 'yes' );
+		wp_set_current_user( 0 );
+
+		$original_post = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Preserve the test request.
+		$_POST         = array( 'createaccount' => '1' );
+		try {
+			$posted_data = $this->sut->get_posted_data();
+		} finally {
+			$_POST = $original_post;
+		}
+
+		$this->assertSame( $expected_createaccount, $posted_data['createaccount'], 'Submitted form data must respect the effective registration setting.' );
+	}
+
 	/**
 	 * Register an extra checkout field in an arbitrary fieldset, the way checkout field editor plugins do.
 	 *
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 63bcdd67d94..7c47c9781b8 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
@@ -101,6 +101,39 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
 		throw new RuntimeException( esc_url_raw( $location ) );
 	}

+	/**
+	 * @testdox My Account registration honors its own merchant setting even when checkout registration is enabled.
+	 *
+	 * @testWith ["no", false]
+	 *           ["yes", true]
+	 *
+	 * @param string $registration_enabled Whether My Account registration is enabled.
+	 * @param bool   $expected_customer Whether a customer should be created.
+	 */
+	public function test_process_registration_respects_myaccount_setting( string $registration_enabled, bool $expected_customer ): void {
+		wp_set_current_user( 0 );
+		update_option( 'woocommerce_enable_myaccount_registration', $registration_enabled );
+		update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
+		add_filter( 'woocommerce_registration_auth_new_customer', '__return_false' );
+		add_filter( 'pre_wp_mail', '__return_true' );
+
+		$_POST             = array(
+			'register'                   => 'Register',
+			'email'                      => 'registration-policy@example.com',
+			'woocommerce-register-nonce' => wp_create_nonce( 'woocommerce-register' ),
+		);
+		$created_customers = did_action( 'woocommerce_created_customer' );
+
+		WC_Form_Handler::process_registration();
+
+		$this->assertSame( $expected_customer, get_user_by( 'email', 'registration-policy@example.com' ) instanceof WP_User, 'The stored customer must reflect the My Account registration setting.' );
+		$this->assertSame( $created_customers + ( $expected_customer ? 1 : 0 ), did_action( 'woocommerce_created_customer' ), 'Disabled registration must not notify customer-creation consumers.' );
+		$this->assertSame( $expected_customer ? 0 : 1, wc_notice_count( 'error' ), 'Only disabled registration should produce an error.' );
+		if ( ! $expected_customer ) {
+			$this->assertTrue( wc_has_notice( 'Registration is disabled.', 'error' ), 'The registration policy error should be shown.' );
+		}
+	}
+
 	/**
 	 * Order statuses when the pay form is posted, and whether pay_action() should take each one on.
 	 *