View Categories

How to change places autocomplete selected API fields

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.

When you click on the checkout Map, Kikote will output the address details sent back by Google to the console. You can dig into this output to see what data Google has about the addresses in your country:

In the above screenshot you can see that there are multiple administrative_area_levels. This is not always the case for other countries. Some countries would only have administrative_area_level_1. So if the address field is not being automatically selected/filled in by default, you can identify which administrative level that Kikote should instead be using.

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',
)