-
-
Save woogists/7327d81dde3370b9c87e13848b70d094 to your computer and use it in GitHub Desktop.
function custom_override_checkout_fields ( $fields ) { | |
$fields['billing']['test_radio']['default'] = 'one'; | |
return $fields; | |
} // End custom_override_checkout_fields() | |
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); |
You should be able to use the e.g. first option as the default like this, so you would not hard code the value in the hook:
$fields['billing']['test_radio']['default'] = array_values($fields['billing']['test_radio']['options'])[0];
strangely this doesn't work for additional fields ie.:
$fields['additional']['test_radio']['default'] = array_values($fields['additional']['test_radio']['options'])[0];
or
$fields['additional']['test_radio']['default'] = 'one';
Same for me, can't get it to work with Additional Fields.
For anyone else that finds themselves here with the same issue. I found that the WooCommerce Checkout Field Editor plugin set a priority of 1000 for their add_filter. So in order to customize things in additional (which is actually order) change the snippet to this which worked for me:
function custom_override_checkout_fields ( $fields ) {
$fields['order']['email_optin']['default'] = 'Yes';
return $fields;
} // End custom_override_checkout_fields()
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' , 1100 );
You should be able to use the e.g. first option as the default like this, so you would not hard code the value in the hook:
$fields['billing']['test_radio']['default'] = array_values($fields['billing']['test_radio']['options'])[0];