Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Checkout Field Editor] Setting a default value for a radio button field
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' );
@kontur
Copy link

kontur commented Jan 16, 2020

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];

Copy link

ghost commented Dec 8, 2020

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';

@gsibert
Copy link

gsibert commented Jul 19, 2022

Same for me, can't get it to work with Additional Fields.

@gsibert
Copy link

gsibert commented Jul 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment