Why Set Default Values in Checkout form?
- Improve conversion
- Saves time for returning users
- Speed up checkout
- Improves form usability and reduces cart abandonment
- Helps with localization (like default country or state)
- Can auto-fill values based on context (user role, geolocation, etc.)
- Provide better personalization
To set default values in Woocommerce checkout form Use Below Code
Add code to your child theme’s functions.php
file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php
file as this will be wiped entirely when you update the theme.
Set Default Country and State
add_filter('woocommerce_checkout_fields', 'set_default_checkout_country_state');
function set_default_checkout_country_state($fields) {
$fields['billing']['billing_country']['default'] = 'US'; // United States
$fields['billing']['billing_state']['default'] = 'CA'; // California
return $fields;
}
Advanced Example: Set Defaults Based on User Role
add_filter('woocommerce_checkout_fields', 'custom_checkout_defaults_by_role');
function custom_checkout_defaults_by_role($fields) {
$user = wp_get_current_user();
if (in_array('wholesale_customer', $user->roles)) {
$fields['billing']['billing_company']['default'] = 'Default Wholesale Co.';
$fields['billing']['billing_city']['default'] = 'New York';
} elseif (in_array('subscriber', $user->roles)) {
$fields['billing']['billing_first_name']['default'] = 'Valued';
$fields['billing']['billing_last_name']['default'] = 'Subscriber';
}
return $fields;
}
Pre-fill Logged-in User’s Data as Default
WooCommerce already does this, but you can override or enhance it:
add_filter('woocommerce_checkout_fields', 'custom_checkout_prefill_from_user');
function custom_checkout_prefill_from_user($fields) {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$fields['billing']['billing_email']['default'] = $user_info->user_email;
$fields['billing']['billing_first_name']['default'] = get_user_meta($user_id, 'first_name', true);
$fields['billing']['billing_last_name']['default'] = get_user_meta($user_id, 'last_name', true);
}
return $fields;
}
Setting Default for Custom Checkout Fields
If you’ve added custom checkout fields, you can also set their default values:
add_filter('woocommerce_checkout_fields', 'custom_default_checkout_field');
function custom_default_checkout_field($fields) {
$fields['billing']['billing_reference_number'] = array(
'type' => 'text',
'label' => __('Reference Number'),
'required' => false,
'default' => 'REF-' . time(),
'priority' => 110,
);
return $fields;
}