How to change places autocomplete selected API fields

< 1 min read

Depending on your country, the default fields that Kikote pulls from the Google Places API might not be ideal for you. For example, the default administration is set to administrative_area_level_1 however, in Spain, the “Province” checkout field would not be automatically filled in because Spain uses administrative_area_level_2.

It’s possible to set which fields get used with the snippet below:

PHP
function sl_cc_change_autocomplete_administration_level($mappings){
$mappings['stateCounty'] = "administrative_area_level_2";
return	$mappings;
}
add_filter('places_address_components_mappings', 'sl_cc_change_autocomplete_administration_level', 20);

The Google Places API fields are currently mapped as follows:

PHP
array(
'country'     => 'country',
'townCity1'   => 'locality',
'townCity2'   => 'postal_town',
'townCity3'   => 'sublocality_level_1',
'stateCounty' => 'administrative_area_level_1',
'zipcode'     => 'postal_code',
)