WooCommerce: No Default Payment @ Checkout Page

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!

With the snippet below, whenever the Checkout page is accessed, there won’t be any pre-selected payment option. User is now forced to click on one of them, otherwise they’ll get an error.

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();

		});
	" );
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *