Change the default selected payment gateway in Woocommerce

Mahmudul Hasan picture Mahmudul Hasan · May 9, 2018 · Viewed 16.5k times · Source

I have two different payment gateway (stripe and bank transfer) in Woocommerce checkout page. But "Bank transfer" (bacs) is always auto selected by default.

Here is screenshot of the payment gateways on my checkout page:

enter image description here

I would like to change that and auto select stripe payment gateway intead.

How can I do it?. Any help is appreciated.

Answer

LoicTheAztec picture LoicTheAztec · May 9, 2018

Updated

You can try to add the following code, to change the default payment gateway on checkout page. You will have to define the default desired payment gateway ID, in this code:

add_action( 'template_redirect', 'define_default_payment_gateway' );
function define_default_payment_gateway(){
    if( is_checkout() && ! is_wc_endpoint_url() ) {
        // HERE define the default payment gateway ID
        $default_payment_id = 'stripe';

        WC()->session->set( 'chosen_payment_method', $default_payment_id );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

You will always get Stripe as default now:

enter image description here


To get the needed payment gateway ID for Stripe , go in Woocommerce > Settings > Checkout and find it in the "Gateway ID" column as in this screenshot:

enter image description here