2

I can find many related questions but the given solutions are outdated and doesn't work anymore. The most common solution I could find is:

add_filter( 'pre_option_woocommerce_default_gateway' . '__return_false', 99 );

but this doesn't work with the latest versions of WooCommerce.

I have also tried to set an empty value in WooCommerce session:

WC()->session->set( 'chosen_payment_method', '' );

This is not working either, woocommerce will select by default the first gateway listed in checkout anyways...

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
svelandiag
  • 4,231
  • 1
  • 36
  • 72

1 Answers1

1

As pre_option_woocommerce_default_gateway hook doesn't exist anymore, you can also try the following, that will reset the chosen payment gateway to the first displayed payment method:

add_action( 'wp_footer', 'default_payment_gateway' );
function default_payment_gateway() {
    if( is_checkout() && ! is_wc_endpoint_url() ) :

    $available_gateways     = WC()->payment_gateways->get_available_payment_gateways();
    $available_gateways_ids = array_keys($available_gateways);
    $default_gateway_id     = reset($available_gateways_ids);

    ?>
    <script language="javascript">
    jQuery( function($){
        var a = '<?php echo $default_gateway_id; ?>',
            b = 'input[name="payment_method"][value="'+a+'"]';

            $(b).prop('checked', true);
            $(document.body).trigger('update_checkout');
    });
    </script>
    <?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme). Works on all WooCommerce versions since 2.5.x.

Or you can set a default specific payment method, replacing:

    $available_gateways     = WC()->payment_gateways->get_available_payment_gateways();
    $available_gateways_ids = array_keys($available_gateways);
    $default_gateway_id     = reset($available_gateways_ids);

by (here "COD" for example):

    $default_gateway_id = 'cod';

Anyways, on radio buttons, the default behavior is always to display the first item as checked. To have all radio buttons unchecked is something unusual.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399