Reorder the Billing and Shipping Fields
Changes the order of checkout address fields, for example country first and postcode before city, to match how addresses are written in your market.
Opens WPIDE on
Code
$wpide_field_order = array( // Field => priority (lower = higher up).
'country' => 5,
'first_name' => 10,
'last_name' => 20,
'company' => 30,
'postcode' => 40,
'city' => 45,
'address_1' => 50,
'address_2' => 60,
'state' => 70,
);
add_filter( 'woocommerce_default_address_fields', function ( $fields ) use ( $wpide_field_order ) {
foreach ( $wpide_field_order as $key => $priority ) {
if ( isset( $fields[ $key ] ) ) {
$fields[ $key ]['priority'] = $priority;
}
}
return $fields;
}, 20 );
add_filter( 'woocommerce_get_country_locale', function ( $locales ) use ( $wpide_field_order ) {
foreach ( $locales as $country => $fields ) {
foreach ( $fields as $key => $field ) {
if ( isset( $field['priority'], $wpide_field_order[ $key ] ) ) {
$locales[ $country ][ $key ]['priority'] = $wpide_field_order[ $key ];
}
}
}
return $locales;
}, 20 );
Notes
Lower numbers come first; field keys have no billing_ or shipping_ prefix and apply to both address forms.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.