Autofill state and postcode according to the city in magento checkout page

Autofill state and postcode according to the city in magento checkout page

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Autofill state and postcode according to the city in magento checkout page

Automatically Filling Postcode and State Based on City During Magento Checkout

Hello developers! I’ve seen many queries around this common issue in Magento – how to automatically populate the postcode and state fields during checkout based on the city entered by the customer. Today, we’ll dive into a practical solution for achieving this functionality.

The Challenge: Integrating Geolocation Data with Checkout Form

In order to auto-fill these fields, we need to integrate geolocation data from an external API like Google Maps or a local database of Australian postcodes and states. The challenge lies in making the integration seamless within Magento’s checkout process without slowing down performance.

A Step-by-Step Solution: Customizing Magento Checkout

Here’s a high-level approach for implementing this feature using custom code:

  1. Fetch Geolocation Data: Use an API like Google Maps Geocoding API to fetch latitude and longitude coordinates based on the city entered by the customer. You can call this API from within your checkout form.

  2. Reverse Geocode: Utilize a reverse geocoding service (again, you could use Google Maps) to retrieve the postcode and state based on the latitude and longitude obtained in step 1.

  3. Store Data Temporarily: Store the fetched data temporarily in a session variable or cache for quick access during checkout.

  4. Populate Form Fields: In your checkout form template (onepage.phtml), pre-fill the postcode and state fields with the data retrieved from the geolocation services.

A Code Example: Fetching Geolocation Data

Here’s a simplified example of how you might fetch geocode data using Google Maps API in your checkout controller:

public function getGeocodeAction()
{
    $city = $this->getRequest()->getParam('city');
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($city) . "&sensor=false&key=YOUR_API_KEY";
    $response = file_get_contents($url);
    $data = json_decode($response, true);

    if (isset($data['results'][0]['geometry']['location'])) {
        $lat = $data['results'][0]['geometry']['location']['lat'];
        $lng = $data['results'][0]['geometry']['location']['lng'];

        // Further processing to fetch postcode and state
    }
}

Conclusion: Optimizing Performance

Remember, when working with external APIs in this manner, it’s crucial to optimize performance:

  • Implement caching for frequent data requests.
  • Use session variables or local storage for temporary data.
  • Limit API calls to necessary fields only.

We recommend exploring extensions like Postcode Lookup by Fontis which can provide a more optimized solution. However, feel free to customize the code above according to your specific requirements and Magento version.

Let me know if you have any questions or need further clarification on implementing this feature in your Magento store!

Checkout Cache Database Performance

Our network