Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default selected payment gateway in Woocommerce

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.

like image 742
Mahmudul Hasan Avatar asked May 09 '18 03:05

Mahmudul Hasan


1 Answers

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

like image 156
LoicTheAztec Avatar answered Oct 08 '22 14:10

LoicTheAztec