From 6aed193a26d20937b5678a755973b4b66bfa58aa Mon Sep 17 00:00:00 2001 From: David Stone Date: Tue, 14 Apr 2026 09:30:47 -0600 Subject: [PATCH] fix: preserve saved gateway and skip gateway pre-selection on free carts When multiple gateways are active, the previous code dropped any gateway the user had already selected during a multi-step checkout redirect. It also pre-selected a paid gateway on free carts when exactly one gateway was configured. Fix: check should_collect_payment() before auto-selecting a gateway, restore a previously saved gateway from the session/request when it is still in the active-gateway list, and only fall back to auto-selecting when exactly one gateway exists. Resolves #833 --- inc/checkout/class-checkout.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/inc/checkout/class-checkout.php b/inc/checkout/class-checkout.php index 8512b5a1a..b4baca96a 100644 --- a/inc/checkout/class-checkout.php +++ b/inc/checkout/class-checkout.php @@ -1963,12 +1963,23 @@ public function get_checkout_variables() { /* * Get the default gateway. * - * Only pre-select when there is exactly one active gateway so - * the user is not surprised by branded buttons (e.g. PayPal) - * before they have made a choice. + * Restore a previously-chosen gateway from the session (multi-step + * checkout), or auto-select only when there is exactly one active + * gateway and the cart actually requires payment. This prevents + * branded buttons (e.g. PayPal) from appearing before the user has + * made a choice, and avoids pre-selecting a paid gateway on free carts. */ $active_gateways = array_keys(wu_get_active_gateway_as_options()); - $default_gateway = count($active_gateways) === 1 ? current($active_gateways) : ''; + $saved_gateway = $this->request_or_session('gateway', ''); + $default_gateway = ''; + + if ($this->should_collect_payment()) { + if ($saved_gateway && in_array($saved_gateway, $active_gateways, true)) { + $default_gateway = $saved_gateway; + } elseif (1 === count($active_gateways)) { + $default_gateway = current($active_gateways); + } + } $d = wu_get_site_domain_and_path('replace');