0

I need to get the region name based on ZIP/Postal code. My code is below but its not working. Can anyone please help to get the region name by zip code?

$postcode = "560100";
$regionCollection = $this->regionCollectionFactory->create();
$regionCollection->addRegionCodeFilter($postcode)->setPageSize(1);

if ($regionCollection->getSize()) {
    $region = $regionCollection->getFirstItem();
}

Thanks in Advance!

5
  • 2
    I think it's not possible with Magento Code you need to call Custom Google API Commented Jun 21, 2023 at 13:22
  • Thak you @RonakRathod for replying. Could you please highlight some examples?
    – MagentoDev
    Commented Jun 21, 2023 at 13:34
  • 1
    Can you please check this : magento.stackexchange.com/questions/197473/…
    – Rana Zain
    Commented Jun 21, 2023 at 13:38
  • @RanaZain I have tried this now and it's not working with the zip code. It seems the method loadByCode will only work with region code parameter and not zip codes. Please correct me if am wrong.
    – MagentoDev
    Commented Jun 21, 2023 at 13:52
  • 1
    Yes, I added an answer can you please check? Commented Jun 22, 2023 at 7:04

1 Answer 1

1

It's not possible with Magento Code you need to call Custom Google API

Here is the Example with the Google Map API

How to get Google API Key?

You need to call the API for getting Region Name by Zip-Code

API : https://maps.googleapis.com/maps/api/geocode/json?address=560100&sensor=true&key=AIzaSyCQ688uhIYTXi4kphAvgTq0Lf25dg62198

Response:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "560100",
               "short_name" : "560100",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Bengaluru",
               "short_name" : "Bengaluru",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bengaluru Urban",
               "short_name" : "Bengaluru Urban",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Bangalore Division",
               "short_name" : "Bangalore Division",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Karnataka",
               "short_name" : "KA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bengaluru, Karnataka 560100, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 12.8751739,
                  "lng" : 77.71305129999999
               },
               "southwest" : {
                  "lat" : 12.8193509,
                  "lng" : 77.6091
               }
            },
            "location" : {
               "lat" : 12.8498481,
               "lng" : 77.6544856
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 12.8751739,
                  "lng" : 77.71305129999999
               },
               "southwest" : {
                  "lat" : 12.8193509,
                  "lng" : 77.6091
               }
            }
         },
         "place_id" : "ChIJCa-XPptsrjsRL5utZGjUjOk",
         "postcode_localities" : [
            "Andapura",
            "Basapura",
            "Bengaluru",
            "Beratena Agrahara",
            "Bettadasanapura",
            "Bhovi Palya",
            "Bommasandra",
            "Bommasandra Industrial Area",
            "Chikkathoguru",
            "Chinthalamadivala",
            "Daadys Gaarden Layout",
            "Doddanagamangala Village",
            "Doddathoguru",
            "Electronic City",
            "Gottamaranahalli",
            "Gulimangala",
            "Hulimangala",
            "LHR layout",
            "Maragondanahalli",
            "Mylasandra",
            "Podu",
            "RS Gardens",
            "Rayasandra",
            "S.Bingipura",
            "Singena Agrahara",
            "Thirupalya",
            "Vaddara Palya",
            "Vidyanagar",
            "Vittasandra"
         ],
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

Note : You need to change key with your API key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.