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');