Hi @alphax,
You’re correct, the WC logic is coded so that the first payment method on your checkout page is the default selected option for a guest user. For returning customer’s it’s based on the payment method used most recently.
You can control the order of the payment methods on the WooCommerce > Settings > Payments page. The listed payment methods are can be dragged into the order you prefer.
Kind Regards,
Thread Starter
alphax
(@alphax)
Hi Clayton,
thanks a lot for the prompt response.
I’m aware of the reordering, however, I’m wondering if it’s possible to not preselect a payment method and show (guest) users an unselected list of payment methods – or if not possible, if that could be integrated into your plugin?
Many thanks
Hi @alphax,
It is possible to show an unselected list but would require some custom code on your end to achieve that.
I am not comfortable adding a feature like that to the plugin because I believe it could step on the toes of other plugins.
Here is an example of how you can achieve what you’re looking for:
add_action('woocommerce_review_order_before_payment', function(){
$current = WC()->session->get( 'chosen_payment_method' );
$gateways = WC()->payment_gateways()->payment_gateways()
if($current && isset($gateways[$current])){
$gateway = $gateways[$current];
$gateway->chosen = false;
}
});
This is an example, be sure and test before implementing.
Kind Regards,
Thread Starter
alphax
(@alphax)
Hi Clayton,
thanks a lot.
I have tested this by adding it to my child theme function (note: there is a semi-colon missing on row 3) but it doesn’t do anything (PayPal is still expanded, i.e. preselected). Would I need to do anything else?
Also, you mentioned it could cause issues with other plugins – you mean other payment plugin or even the payment gateways? I thought given the user is still making a selection, it should be fine?
Many thanks
Also, you mentioned it could cause issues with other plugins – you mean other payment plugin or even the payment gateways?
The standard WC behavior is to pre-select a payment option so if you’re deviating from that it can cause unintended issues. For example, some other gateway’s JS code may depend on a payment option being pre-selected on the checkout page.
The reason it’s not working is I just reviewed the WC checkout.js file and if there are no selected payment methods, it pre-selects the first one. That’s hard coded into the WC code base so there’s no changing that. You could enqueue your own script but that’s doing quite a bit.
Kind Regards,
Thread Starter
alphax
(@alphax)
Okay, thanks for checking and confirming.
I’ll leave it preselected!