From 5209e27de54e6d9e1a1dca8477c435550e7ec9f6 Mon Sep 17 00:00:00 2001 From: David Stone Date: Tue, 14 Apr 2026 09:27:00 -0600 Subject: [PATCH] fix(checkout): preserve saved gateway and skip paid gateways on free carts When deciding $default_gateway in get_vars(), two edge cases were missed: 1. A gateway previously saved in the signup session was dropped on multi-step redirects when multiple gateways were active. 2. A free cart with exactly one paid gateway still had that gateway pre-selected, reintroducing the branded-button state PR #830 aimed to remove. Fix: check should_collect_payment() before setting any default; within that guard, prefer the saved session gateway (validated against active gateways) and only fall back to auto-selecting the single active gateway. Resolves #838 --- 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..4e454e901 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. + * Preserve any previously saved gateway from the signup session + * so multi-step redirects don't lose the user's earlier choice. + * When no saved gateway is present, only pre-select when there is + * exactly one active gateway AND the cart requires payment — paid + * gateways must never be seeded for free checkouts. */ $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');