0

I want to add city drop down on address book page in my account

public function citiesJson()
    {
        $countriesJson = $this->helperData->getRegionJson();
        $countriesArray = json_decode($countriesJson, true);

        $searchCriteriaBuilder = $this->searchCriteria;
        $searchCriteria = $searchCriteriaBuilder->create();

        $citiesList = $this->customCityRepository->getList($searchCriteria);
        $items = $citiesList->getItems();


        foreach ($items as $item) {
            $citiesData[$item->getEntityId()] = $item;
        }

        $countriesArrayUpdated = [];
        foreach ($countriesArray as $key => $country) {
            if ($key == 'config') {
                $countriesArrayUpdated[$key] = $country;
            }

            $regions = [];
            foreach ($country as $regionId => $region) {
                foreach ($citiesData as $cityId => $cityData) {
                    $entityId = $cityData->getRegionId();
                    if ($entityId == $regionId) {
                        $id       = $cityData->getId();
                        $cityName = $cityData->getCityName();
                        $region['cities'][$id] = [
                            'name' => (string)__($cityName),
                            'id' => $cityId
                        ];
                    }
                }

                $regions[$regionId] = $region;
            }
            $countriesArrayUpdated[$key] = $regions;
        }

        $countriesJsonUpdated = json_encode($countriesArrayUpdated);

        return $countriesJsonUpdated;
    }

I have inject the block function in my edit.phtml:-

$blockObj = $block->getLayout()->createBlock('Vendor\Module\Block\Checkout\Cities');

            $cities = $blockObj->citiesJson();
            echo "<pre>"; print_r($cities); echo "</pre>";

My Json:

{
  "config": {
    "show_all_regions": true,
    "regions_required": [
      "AT",
      "BR",
      "CA",
      "HR",
      "EE",
      "FI",
      "IN",
      "LV",
      "LT",
      "RO",
      "SA",
      "ES",
      "CH",
      "US"
    ]
  },
  "SA": {
    "569": {
      "code": "AB",
      "name": "Al Bahah",
      "cities": {
        "13656": {
          "name": "Al Bahah11",
          "id": 13656
        },
        "13657": {
          "name": "Al Bahah22",
          "id": 13657
        },
        "13658": {
          "name": "Test",
          "id": 13658
        }
      }
    },
    "570": {
      "code": "AJ",
      "name": "Al Jawf",
      "cities": []
    },
    "571": {
      "code": "AL",
      "name": "Al Jezera",
      "cities": {
        "13659": {
          "name": "Test",
          "id": 13659
        }       
      }
    }
  }
}

Now I want to use this JSON to create city dropdown in address book page when I select SA country and state like below:-

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.