Commit e22780bbcaa for woocommerce

commit e22780bbcaa29c9d03d79a80221d5b237aea3854
Author: James Kemp <me@jckemp.com>
Date:   Fri Dec 4 11:01:25 2026 +0000

    Fix payment methods disappearing when gateways share an order number (#71324)

    * Fix payment methods disappearing when gateways share an order number

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Fix failing CI: PHP: 8.5 WP: latest [WP latest] - @woocommerce/plugin-woocommerce [unit:php], PHP: 8.3 WP: latest [WP la

    * Stop saved gateway positions overflowing past PHP_INT_MAX in init()

    * Add changefile(s) from automation for the following project(s): woocommerce

    * Append gateways with no free position after sorting in init()

    * Sort payment gateways by saved position and option key order in init()

    * List twice-registered gateways once and keep float positions in ordering

    * Match init() tie and unsaved ordering in get_checkout_order()

    ---------

    Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/71324-fix-71135-payment-methods-disappear-from-checkout b/plugins/woocommerce/changelog/71324-fix-71135-payment-methods-disappear-from-checkout
new file mode 100644
index 00000000000..4d61f8a8ac1
--- /dev/null
+++ b/plugins/woocommerce/changelog/71324-fix-71135-payment-methods-disappear-from-checkout
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix payment methods disappearing from checkout when several of them share the same position in the payment order.
diff --git a/plugins/woocommerce/includes/class-wc-payment-gateways.php b/plugins/woocommerce/includes/class-wc-payment-gateways.php
index 03cddeb2414..fd7ac16dd82 100644
--- a/plugins/woocommerce/includes/class-wc-payment-gateways.php
+++ b/plugins/woocommerce/includes/class-wc-payment-gateways.php
@@ -26,6 +26,8 @@ class WC_Payment_Gateways {
 	/**
 	 * Payment gateway classes.
 	 *
+	 * A sorted list: the keys are sequential (0..n-1) and do not reflect saved positions.
+	 *
 	 * @var WC_Payment_Gateway[]
 	 */
 	public $payment_gateways = array();
@@ -104,8 +106,12 @@ class WC_Payment_Gateways {
 		);

 		// Get sort order option.
-		$ordering  = (array) get_option( 'woocommerce_gateway_order' );
-		$order_end = 999;
+		$ordering    = (array) get_option( 'woocommerce_gateway_order' );
+		$key_indexes = array_flip( array_keys( $ordering ) );
+		$order_end   = 999;
+
+		// Collect gateways with their sort keys, so a re-init overwrites, rather than appends to, the previous gateways.
+		$sortable = array();

 		// Load gateways in order.
 		foreach ( $load_gateways as $gateway ) {
@@ -126,16 +132,28 @@ class WC_Payment_Gateways {
 			}

 			if ( isset( $ordering[ $gateway->id ] ) && is_numeric( $ordering[ $gateway->id ] ) ) {
-				// Add in position.
-				$this->payment_gateways[ $ordering[ $gateway->id ] ] = $gateway;
+				$position  = $ordering[ $gateway->id ] + 0;
+				$key_index = $key_indexes[ $gateway->id ];
 			} else {
-				// Add to end of the array.
-				$this->payment_gateways[ $order_end ] = $gateway;
+				// Gateways without a saved position go to the end, after any saved gateway at the same position.
+				$position  = $order_end;
+				$key_index = PHP_INT_MAX;
 				++$order_end;
 			}
+
+			// Keyed by ID, so the last loaded copy of a gateway wins.
+			$sortable[ $gateway->id ] = array( $position, $key_index, $gateway );
 		}

-		ksort( $this->payment_gateways );
+		// Break ties by option key order, as the Payments settings page does when it normalizes the option.
+		usort(
+			$sortable,
+			static function ( $a, $b ) {
+				return array( $a[0], $a[1] ) <=> array( $b[0], $b[1] );
+			}
+		);
+
+		$this->payment_gateways = array_column( $sortable, 2 );

 		add_action( 'wc_payment_gateways_initialized', array( $this, 'on_payment_gateways_initialized' ) );
 		/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewayUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewayUpdate.php
index c4864d91a2b..71ace9d0f1b 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewayUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/PaymentGatewayUpdate.php
@@ -263,24 +263,35 @@ class PaymentGatewayUpdate extends AbstractDomainAbility implements AbilityDefin

 	/**
 	 * Mirrors how WC_Payment_Gateways::init() sorts gateways: by their saved
-	 * order, with unordered gateways after them. The loaded gateways keep the
-	 * order they had when they loaded, even after a move in the same request.
+	 * order, with unordered gateways numbered from 999 in load order. The loaded
+	 * gateways keep the order they had when they loaded, even after a move in the same request.
+	 * Tied saved gateways follow the option key order, ahead of unordered ones.
 	 *
 	 * @param array<int, string> $ids Gateway IDs.
 	 * @return array<int, string>
 	 */
 	private static function get_checkout_order( array $ids ): array {
-		$saved     = (array) get_option( PaymentsProviders::PROVIDERS_ORDER_OPTION );
-		$positions = array();
-		$end       = PHP_INT_MAX - count( $ids );
+		$saved       = (array) get_option( PaymentsProviders::PROVIDERS_ORDER_OPTION );
+		$key_indexes = array_flip( array_keys( $saved ) );
+		$end         = 999;
+		$sortable    = array();

 		foreach ( $ids as $gateway_id ) {
-			$positions[ $gateway_id ] = isset( $saved[ $gateway_id ] ) && is_numeric( $saved[ $gateway_id ] ) ? (int) $saved[ $gateway_id ] : $end++;
+			if ( isset( $saved[ $gateway_id ] ) && is_numeric( $saved[ $gateway_id ] ) ) {
+				$sortable[] = array( $saved[ $gateway_id ] + 0, $key_indexes[ $gateway_id ], $gateway_id );
+			} else {
+				$sortable[] = array( $end++, PHP_INT_MAX, $gateway_id );
+			}
 		}

-		asort( $positions );
+		usort(
+			$sortable,
+			static function ( $a, $b ) {
+				return array( $a[0], $a[1] ) <=> array( $b[0], $b[1] );
+			}
+		);

-		return array_map( 'strval', array_keys( $positions ) );
+		return array_column( $sortable, 2 );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/helpers/CorePayPalGatewayTrait.php b/plugins/woocommerce/tests/php/helpers/CorePayPalGatewayTrait.php
index b3067a7f469..cb3f3d882ba 100644
--- a/plugins/woocommerce/tests/php/helpers/CorePayPalGatewayTrait.php
+++ b/plugins/woocommerce/tests/php/helpers/CorePayPalGatewayTrait.php
@@ -77,10 +77,6 @@ trait CorePayPalGatewayTrait {

 	/**
 	 * Rebuild the loaded payment gateways from the current options.
-	 *
-	 * WC_Payment_Gateways::init() adds to the loaded list rather than replacing
-	 * it, so the list has to be emptied first for a gateway that is no longer
-	 * loadable to actually disappear.
 	 */
 	protected static function reload_payment_gateways(): void {
 		WC()->payment_gateways()->payment_gateways = array();
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-payment-gateways-test.php b/plugins/woocommerce/tests/php/includes/class-wc-payment-gateways-test.php
index 552973971bb..9c55880b703 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-payment-gateways-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-payment-gateways-test.php
@@ -530,6 +530,190 @@ class WC_Payment_Gateways_Test extends WC_Unit_Test_Case {
 		$this->assertEquals( 'nonexistent_gateway', $result );
 	}

+	/**
+	 * @testdox Should keep every gateway listed when several share the same saved order number.
+	 */
+	public function test_init_keeps_all_gateways_when_saved_order_numbers_collide(): void {
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_BACS::ID   => 0,
+				WC_Gateway_Cheque::ID => 0,
+				WC_Gateway_COD::ID    => 0,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+
+		$gateway_ids = array_map(
+			static function ( $gateway ) {
+				return $gateway->id;
+			},
+			array_values( $sut->payment_gateways )
+		);
+
+		$this->assertContains( WC_Gateway_BACS::ID, $gateway_ids, 'Direct bank transfer should not be overwritten.' );
+		$this->assertContains( WC_Gateway_Cheque::ID, $gateway_ids, 'Check payments should not be overwritten.' );
+		$this->assertContains( WC_Gateway_COD::ID, $gateway_ids, 'Cash on delivery should not be overwritten.' );
+		$this->assertSame(
+			array( WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ),
+			array_slice( $gateway_ids, 0, 3 ),
+			'Gateways with the same saved position should follow the option key order.'
+		);
+	}
+
+	/**
+	 * @testdox Should sort gateways by saved position, then by option key order for ties.
+	 */
+	public function test_init_sorts_by_saved_position_then_option_key_order(): void {
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_Cheque::ID => 0,
+				WC_Gateway_BACS::ID   => 0,
+				WC_Gateway_COD::ID    => 1,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+		$ids = wp_list_pluck( $sut->payment_gateways, 'id' );
+		$this->assertSame( array( WC_Gateway_Cheque::ID, WC_Gateway_BACS::ID, WC_Gateway_COD::ID ), array_slice( $ids, 0, 3 ), 'Tied gateways follow the option key order.' );
+
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_BACS::ID   => 2,
+				WC_Gateway_Cheque::ID => 0,
+				WC_Gateway_COD::ID    => 1,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+		$ids = wp_list_pluck( $sut->payment_gateways, 'id' );
+		$this->assertSame( array( WC_Gateway_Cheque::ID, WC_Gateway_COD::ID, WC_Gateway_BACS::ID ), array_slice( $ids, 0, 3 ), 'Distinct positions are respected.' );
+
+		update_option( 'woocommerce_gateway_order', array( WC_Gateway_COD::ID => 999 ) );
+
+		$sut = new WC_Payment_Gateways();
+		$ids = wp_list_pluck( $sut->payment_gateways, 'id' );
+		$this->assertSame( array( WC_Gateway_COD::ID, WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID ), array_slice( $ids, 0, 3 ), 'A saved gateway sorts before an unsaved one at the same position.' );
+	}
+
+	/**
+	 * @testdox Should not duplicate gateways when init is called twice.
+	 */
+	public function test_init_called_twice_does_not_duplicate_gateways(): void {
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_BACS::ID   => 0,
+				WC_Gateway_Cheque::ID => 0,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+		$sut->init();
+		$first = wp_list_pluck( $sut->payment_gateways, 'id' );
+
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_Cheque::ID => 0,
+				WC_Gateway_COD::ID    => 1,
+				WC_Gateway_BACS::ID   => 2,
+			)
+		);
+		$sut->init();
+		$second = wp_list_pluck( $sut->payment_gateways, 'id' );
+
+		$this->assertSame( array( WC_Gateway_Cheque::ID, WC_Gateway_COD::ID, WC_Gateway_BACS::ID ), array_slice( $second, 0, 3 ), 'The second init should apply the new order.' );
+		$this->assertEqualsCanonicalizing( $first, $second, 'The second init should list the same gateways.' );
+		$this->assertSame( count( array_unique( $second ) ), count( $second ), 'No gateway should be listed twice.' );
+	}
+
+	/**
+	 * @testdox Should keep every gateway, in option key order, when several are saved at the maximum integer position.
+	 */
+	public function test_init_keeps_all_gateways_when_saved_at_max_int(): void {
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_BACS::ID   => PHP_INT_MAX,
+				WC_Gateway_Cheque::ID => PHP_INT_MAX,
+				WC_Gateway_COD::ID    => PHP_INT_MAX,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+
+		$gateway_ids = array_map(
+			static function ( $gateway ) {
+				return $gateway->id;
+			},
+			array_values( $sut->payment_gateways )
+		);
+
+		$this->assertContains( WC_Gateway_BACS::ID, $gateway_ids );
+		$this->assertContains( WC_Gateway_Cheque::ID, $gateway_ids );
+		$this->assertContains( WC_Gateway_COD::ID, $gateway_ids );
+		$this->assertSame(
+			array( WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ),
+			array_values( array_intersect( $gateway_ids, array( WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ) ) ),
+			'Gateways tied at the maximum position should follow the option key order.'
+		);
+	}
+
+	/**
+	 * @testdox Should keep a gateway without a saved order when an earlier gateway was saved at the end slot.
+	 */
+	public function test_init_keeps_unordered_gateway_when_end_slot_is_taken(): void {
+		// BACS is saved at 999, the position where unordered gateways start, so none of them may overwrite it.
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				WC_Gateway_BACS::ID => 999,
+			)
+		);
+
+		$sut = new WC_Payment_Gateways();
+
+		$gateway_ids = array_map(
+			static function ( $gateway ) {
+				return $gateway->id;
+			},
+			array_values( $sut->payment_gateways )
+		);
+
+		$this->assertSame(
+			array( WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ),
+			array_slice( $gateway_ids, 0, 3 ),
+			'All gateways should be kept, in load order.'
+		);
+		$this->assertSame( count( array_unique( $gateway_ids ) ), count( $gateway_ids ), 'No gateway should be listed twice.' );
+	}
+
+	/**
+	 * @testdox Should list a gateway registered twice only once, keeping the last loaded copy.
+	 */
+	public function test_init_keeps_last_copy_of_gateway_registered_twice(): void {
+		update_option( 'woocommerce_gateway_order', array( WC_Gateway_BACS::ID => 0 ) );
+
+		$first_copy = new WC_Gateway_BACS();
+		$last_copy  = new WC_Gateway_BACS();
+		$register   = static function () use ( $first_copy, $last_copy ) {
+			return array( $first_copy, $last_copy );
+		};
+		add_filter( 'woocommerce_payment_gateways', $register );
+
+		try {
+			$sut = new WC_Payment_Gateways();
+		} finally {
+			remove_filter( 'woocommerce_payment_gateways', $register );
+		}
+
+		$this->assertSame( array( $last_copy ), $sut->payment_gateways, 'Only the last loaded copy should be listed.' );
+	}
+
 	/**
 	 * Enable all payment gateways.
 	 */
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 3cdff36487f..fe3dd651a78 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -6393,7 +6393,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * Rebuild the loaded payment gateways from the current options, since init() only appends.
+	 * Rebuild the loaded payment gateways from the current options.
 	 */
 	private static function reload_payment_gateways(): void {
 		WC()->payment_gateways()->payment_gateways = array();
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/PaymentGatewayUpdateTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/PaymentGatewayUpdateTest.php
index 353833922dd..c33c6f1df49 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/PaymentGatewayUpdateTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/PaymentGatewayUpdateTest.php
@@ -408,6 +408,48 @@ class PaymentGatewayUpdateTest extends \WC_Unit_Test_Case {
 		$this->assertSame( $offline[0], $this->get_saved_positions()['bacs'] );
 	}

+	/**
+	 * @testdox Should move a gateway saved above the maximum integer position first, and report the position checkout lists.
+	 */
+	public function test_moves_a_gateway_saved_above_max_int_first(): void {
+		update_option(
+			'woocommerce_gateway_order',
+			array(
+				'bacs'   => 0,
+				'cheque' => 1,
+				'cod'    => 1.0e19,
+			)
+		);
+		self::reload_payment_gateways();
+
+		$result = $this->move_gateway( 'cod', 0 );
+
+		$listed = array_search( 'cod', wp_list_pluck( WC()->payment_gateways()->payment_gateways, 'id' ), true );
+		$this->assertSame( 0, $listed, 'Checkout should list Cash on delivery first.' );
+		$this->assertSame( $listed, $result['payment_gateway']['order'], 'The response should give the position checkout uses.' );
+	}
+
+	/**
+	 * @testdox Should report the position checkout lists for a gateway saved after the first unordered gateway.
+	 */
+	public function test_reports_the_listed_position_among_unordered_gateways(): void {
+		update_option( 'woocommerce_gateway_order', array( 'bacs' => 1000 ) );
+		self::reload_payment_gateways();
+
+		$result = $this->run_ability(
+			'woocommerce/payment-gateway-update',
+			array(
+				'id'    => 'bacs',
+				'title' => 'Wire transfer',
+			)
+		);
+
+		$this->assertNotWPError( $result );
+		$listed = array_search( 'bacs', wp_list_pluck( WC()->payment_gateways()->payment_gateways, 'id' ), true );
+		$this->assertSame( 1, $listed, 'Checkout should list Direct bank transfer after Check payments.' );
+		$this->assertSame( $listed, $result['payment_gateway']['order'], 'The response should give the position checkout uses.' );
+	}
+
 	/**
 	 * @testdox Should refuse a title that is empty once markup and whitespace are removed, and keep the saved title.
 	 *
@@ -574,7 +616,7 @@ class PaymentGatewayUpdateTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * Rebuild the loaded payment gateways from the current options, since init() only appends.
+	 * Rebuild the loaded payment gateways from the current options.
 	 */
 	private static function reload_payment_gateways(): void {
 		WC()->payment_gateways()->payment_gateways = array();