When you land on the WooCommerce Checkout page, one payment option (radio button) will be selected by default. this is defined based on the last payment method (logged in customer), or the payment gateway sorting (logged out customer).
Often it happens, however, that customers forget to change their payment selection, and therefore end up checking out with the wrong payment option. Which means, more admin work.
With this simple snippet, we will inject some JS in the Woo Checkout page, so that on load, all payment method radio inputs will be unchecked. Super easy!
PHP Snippet: Remove Default Payment Gateway On WooCommerce Checkout Page Load
The purpose of the function is to uncheck the default payment method and hide the corresponding payment method description box if there is more than one payment option available.
In summary, when the WooCommerce checkout page is loaded or updated, this code checks the number of available payment options. If there is more than one option, it unchecks any currently checked payment method and hides the corresponding payment method description box. This can be useful to prevent a default payment method from being pre-selected when there are multiple payment options available during the checkout process.
/**
* @snippet No Default Payment @ Woo Checkout
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community Join https://businessbloomer.com/club/
*/
add_action( 'woocommerce_before_checkout_form', 'bbloomer_uncheck_default_payment_gateway' );
function bbloomer_uncheck_default_payment_gateway() {
wc_enqueue_js( "
// ONLY RUN ON CHECKOUT PAGE LOAD
$( document.body ).on( 'updated_checkout', function() {
// ONLY RUN IF MORE THAN 1 PAYMENT OPTION
if ( $( '.woocommerce-checkout' ).find( 'input[name=\'payment_method\']' ).length === 1 ) return false;
// UNCHECK CHECKED PAYMENT METHOD
$('input[name=\'payment_method\']').prop('checked', false);
// CLOSE CHECKED PAYMENT DESCRIPTION BOX
$('div.payment_box').hide();
});
" );
}