On Checkout page payment methods are presented and the first one is selected by default and automatically. I need to prevent the selection so no payment method is initially selected by WC.
I tried 2 things so far:
-
jQuery from Chrome console:
jQuery( ‘.payment_methods input.input-radio’ ).prop(‘checked’, false);
result:
[<input id=â"payment_method_paypal" type=â"radio" class=â"input-radio" name=â"payment_method" value=â"paypal" data-order_button_text=â"Proceed to PayPal" checked=â"checked">â,
<input id=â"payment_method_accountfunds" type=â"radio" class=â"input-radio" name=â"payment_method" value=â"accountfunds" data-order_button_text>â]
-
Remove the code from payment-method.php Woocommerce template file:
checked( $gateway->chosen, false );
Neither is working. How to do it? Any snippet or suggestion for that, please?
EDIT:
Also tried this:
function wpchris_filter_gateways( $gateways ){
global $woocommerce;
foreach ($gateways as $gateway) {
$gateway->chosen = 0;
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wpchris_filter_gateways', 1);
OK, got it working. Here is how:
/wp-content/plugins/woocommerce/assets/js/frontend/checkout.js
into:
/wp-content/themes/Your-Theme/woocommerce/js/checkout.js
Open that newly created file and search for the following code:
It Should be around line 298. Go ahead and comment it out.
Add this to your functions.php file:
Now, the default payment method should not get checked when you refresh the Checkout page.
I have the same problem and I have solved this way:
Override the checkout-js file with my child theme checkout-override-js.
Then commented out the following code:
line: 48 :
this.init_payment_methods();
line: 58 :
line: 113:
wc_checkout_form.init_payment_methods();
This will remove the default payment method selection. You can play with these code to make it to fit with your requirement.
There is not a good way of achieving this through a filter, this doesn’t work with latest versions of woocommerce 4.x:
Instead of overwriting Woocommerce core JS files as the other anwers suggest, do this instead:
Complementing @Tosh answer, but adding the javascript callback so it runs every time checkout updates through ajax.
Tested and working.
I solved this problem with jQuery. All I do is write a function that is triggered by the scroll gesture after the page has loaded. It really works, anyone can add the following code to the bottom of the
footer.php
file in the theme files. Also, don’t forget to edit thepayment_method_name
according to you.None of these solutions worked well for me.
My solution was:
Let WooCommerce behave like it wants to
I added a payment option (In this case COD) to the top of the list.
With CSS, I hid the entire
<li>
Then I added validation in ‘woocommerce_after_checkout_validation’.
If the user chose this one, I simply returned an error of ‘no payment selected’
Seems to be working just fine.
I used @Viktor BorÃtás answer and worked fine in WooCommerce 6.0.0.
To overridde checkout.min.js and make it independent from updates i used the function below in child theme functions.php file.
I needed to change Woocommerce default payment method on Checkout page (nothing else i tried did the job!) so didn’t commented this code:
Instead i changed the value of payment_methods selection to something else.
Maybe such a crutch will help someone. In the file
payment-method.php
add<input type="radio" class="input-radio" name="payment_method" style="display:none;" checked>
In
payment-method.php
i simply commented out this part (line 23 as of WooCommerce 4.5.2)(If you have child theme, place the modified file into
wp-content/themes/yourchildtheme/woocommerce/checkout/ directory
.)and,
Need to make changes in
checkout.min.js
(atwp-content/plugins/woocommerce/assets/js/frontend/
) too. Commented this out this at line 80 (WooCommerce 4.5.2)This second file unfortunately can’t be overridden simply by copying it into the child theme folder. This solution isn’t update-proof yet. But maybe an OK base to continue with and make it update-proof on a smart and (as) native (as possible) way. Here are some solutions i found for that.
p.s: this solution preserves the selected payment method even if customer changes zip code, address data etc.