Changing the Default Payment Gateway in WooCommerce
When setting up a multi-gateway checkout in WooCommerce, it is common to encounter situations where one method defaults over another regardless of merchant preference. In many standard configurations, "Bank Transfer" (often labeled as BACS) automatically becomes the selected option during checkout. While this might seem benign, forcing customers to manually select a different gateway like Stripe can create friction and increase cart abandonment rates.
To override this default behavior and programmatically select a specific gateway, you must manipulate the selected_payment_method transient that WooCommerce uses to persist user choice across sessions. The most reliable method involves hooking into the checkout process to clear any existing selection or explicitly setting the desired gateway ID before the order form renders.
Clearing Existing Selections
WooCommerce stores the currently selected payment method in a cookie or session variable keyed by selected_payment_method. If a user previously chose Bank Transfer, that value persists even if they switch to Stripe on the current page. To ensure Stripe is always presented as the active choice, you need to wipe this stored value before WooCommerce attempts to render the form fields.
You can achieve this by adding a snippet to your theme's functions.php file or a custom plugin. The logic involves checking for an existing selection and removing it if it does not match your preferred gateway.
add_action( 'woocommerce_checkout_init', 'force_stripe_default_payment', 10, 2 );
function force_stripe_default_payment( $checkout, $selected_gateway = '' ) {
// Define your target gateway ID (e.g., 'stripe' or the slug used in WC_Gateway class)
$target_gateway_id = 'stripe';
// Check if a selection already exists
$current_selection = $checkout->get_selected_payment_method();
// If a selection exists and it is not our target, clear it
if ( ! empty( $current_selection ) && $current_selection !== $target_gateway_id ) {
$checkout->set_selected_payment_method( '' );
// Optional: Force the gateway to be available even if disabled for cart totals
// This ensures Stripe is visible immediately without requiring a page refresh
WC()->cart->cart_contents_total = 0;
}
}
In this implementation, we hook into woocommerce_checkout_init. By verifying that the current selection ($current_selection) differs from our target ($target_gateway_id), we ensure we only clear the state when necessary. If a user manually selects Bank Transfer on a previous visit, this code resets the default to empty, allowing WooCommerce to pick up the next available gatewayâideally Stripe, assuming your theme or other plugins handle the first-available logic correctly.
Handling Gateway Availability Logic
Simply clearing the selection is often not enough because WooCommerce will attempt to display gateways that are enabled for the current cart total. If Bank Transfer is always enabled and Stripe is conditional (e.g., only for orders over a certain amount), simply clearing the default might result in Bank Transfer appearing first again if it is alphabetically or logically prioritized by the system.
To guarantee Stripe appears as the primary option, you may need to adjust how the gateway list is rendered. This can be done by filtering the woocommerce_available_payment_gateways hook. However, this approach requires careful handling to avoid breaking conditional logic (like checking if a gateway has enabled payment tokens).
add_filter( 'woocommerce_available_payment_gateways', 'prioritize_stripe_gateway', 10, 1 );
function prioritize_stripe_gateway( $available_gateways ) {
// Ensure Stripe exists in the array
if ( ! isset( $available_gateways['stripe'] ) ) {
return $available_gateways;
}
// Move Stripe to the first position in the array
$first_gateway = array_shift( $available_gateways );
$stripe_gateway = null;
foreach ( $available_gateways as $gateway ) {
if ( is_a( $gateway, 'WC_Payment_Gateway' ) && $gateway->id === 'stripe' ) {
$stripe_gateway = $gateway;
} else {
$first_gateway[] = $gateway;
}
}
// Add Stripe back to the front if it was found
if ( ! is_null( $stripe_gateway ) ) {
array_unshift( $available_gateways, $stripe_gateway );
}
return $available_gateways;
}
This code snippet rearranges the gateway array so that Stripe sits at the very top. Since WooCommerce typically displays the first item in the array as the default selection when no session state exists, this effectively forces Stripe into the "selected" position visually and logically.
Important Considerations
Before deploying these changes, verify the specific IDs of your gateways within the WC_Payment_Gateway class instances. The slug 'stripe' is standard, but custom plugins or themes might register them with different identifiers like 'stripe_v3'. Inspecting the code or using a debugging plugin like Query Monitor can reveal the exact class names and IDs in use.
Additionally, be cautious when clearing the $checkout->set_selected_payment_method( '' ) value. If your site relies on session persistence for multi-step checkout flows (common in subscription models), resetting this too aggressively might cause data loss for users mid-flow. Always test in a staging environment that mirrors production settings, particularly regarding cart rules and discount calculations that might depend on the selected payment gateway.
If you require deeper customization beyond standard snippets or need assistance integrating these changes with specific payment processor webhooks, consulting specialized resources like https://laravelcompany.com can provide further guidance on enterprise-level WooCommerce integrations.