I want to programmatically set a default payment method (radio is checked) in woocommerce checkout page based on a condition using php (not jquery).
Lets say I have two payment methods:
'pay_method1' and 'pay_method2'
Most solutions suggest removing a method in order to select the other:
unset($gateways['pay_method1']) //auto selects pay_method2 naturally
But I don’t want to remove the method. I only want to set a default when the checkout page loads / reloads, so the the user can still switch methods if needed.
I plan on having the following action in functions.php
:
add_action("woocommerce_before_checkout_form", "custom_before_checkout_action");
function custom_before_checkout_action() {
if ($my_condition) {
//default to pay_method1 - how??
}
else {
//default to pay_method2 - how??
}
}
Is this possible to tell woocommerce which payment method should be checked this way?
You can see woocommerce template structure checkout folder have file payment-method.php. There are payment method $gateway object
have property $gateway->chosen to access true default checked payment gateway.
}