diff --git a/Block/Checkout/CityUpdater.php b/Block/Checkout/CityUpdater.php new file mode 100644 index 0000000..da16df7 --- /dev/null +++ b/Block/Checkout/CityUpdater.php @@ -0,0 +1,59 @@ +searchCriteria = $searchCriteria; + $this->romCityRepository = $romCityRepository; + $this->serializer = $serializer; + parent::__construct($context, $data); + } + + public function citiesJson() + { + + $searchCriteriaBuilder = $this->searchCriteria; + $searchCriteria = $searchCriteriaBuilder->create(); + + $citiesList = $this->romCityRepository->getList($searchCriteria); + $items = $citiesList->getItems(); + + $return = []; + + /** @var RomCity $item */ + foreach ($items as $item) { + $return[] = ['region_id' => $item->getRegionId(), 'city_name' => $item->getCityName()]; + } + + return $this->serializer->serialize($return); + } +} diff --git a/composer.json b/composer.json index 8d12644..3baf5ff 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "eadesignro/romcity", "description": "A module that adds checkout city field to checkout quote shipping address", "type": "magento2-module", - "version": "2.0.0", + "version": "2.0.1", "autoload": { "files": [ "registration.php" diff --git a/view/adminhtml/web/js/city-admin.js b/view/adminhtml/web/js/city-admin.js index 04f78b5..32f1873 100644 --- a/view/adminhtml/web/js/city-admin.js +++ b/view/adminhtml/web/js/city-admin.js @@ -11,65 +11,64 @@ define([ var string = JSON.stringify($eaCitiesJson), obj = JSON.parse(string), - romania = obj.RO; + cityInput = $("[name*='city']").val(); - $(document).on('change', "[name*='region_id']", function () { - var region_id = $(this).val(), - region = romania[region_id], - regionName = this.name, - cityInputName = regionName.replace("region_id", "city"); + $(document).ready(function (){ + var region_id = $("[name*='region_id']").val(); + var region = []; if (region_id) { - var city = $("[name*='" + cityInputName + "']"), - cityHtml = city.parent().html(), - selectCity = cityHtml.replace("input", "select") + '', - cityObject = $(selectCity), - htmlSelect = '', - cityName, - options, - selectOptions, - initialInput = $('', { - 'class': 'admin__control-text', - 'type': 'text', - 'data-bind': 'value:value,' + - 'hasFocus:focused,' + - 'valueUpdate: valueUpdate,' + - 'attr: {' + - 'name: inputName,' + - 'placeholder: placeholder,' + - 'id: uid,' + - 'disabled: disabled,' + - 'maxlength: 255,' + - 'event: {change: userChanges}', - 'name': cityInputName, - 'maxlength': '255' - }); - - cityObject.empty(); + $.each(obj, function (index, value) { + if (value.region_id == region_id) { + region.push(value.city_name); + } + }); + var city = $("[name*='city']"), + selectCity = city.replaceWith("', + htmlSelect = '', + options; $.each(region, function (index, value) { - if ($.isPlainObject(value)) { - $.each(value, function (index, romCity) { + if ( value == cityInput) { + options = ''; + } else { + options = ''; + } + + htmlSelect += options; + }); - cityName = romCity.name; - options = ''; - htmlSelect += options; + $('#city').append(htmlSelect); + } - }) + }); + + $(document).on('change', "[name*='region_id']", function () { + + var region_id = $(this).val(), + regionName = this.name, + cityInputName = regionName.replace("region_id", "city"), + region = []; + + if (region_id) { + $.each(obj, function (index, value) { + if (value.region_id == region_id) { + region.push(value.city_name); } }); + var city = $("[name*='" + cityInputName + "']"), + selectCity = city.replaceWith("', + htmlSelect = '', + options; - selectOptions = cityObject.append(htmlSelect); + $.each(region, function (index, value) { + options = ''; + htmlSelect += options; + }); - if (region.cities === undefined) { - return; - } - if (Object.keys(region.cities).length !== 0) { - city.replaceWith(selectOptions); - } else { - city.replaceWith(initialInput); - } + $('#city').append(htmlSelect); } + }); }; -}); \ No newline at end of file +}); diff --git a/view/frontend/web/js/city-update.js b/view/frontend/web/js/city-update.js index 3b8dd4f..cffd2dd 100644 --- a/view/frontend/web/js/city-update.js +++ b/view/frontend/web/js/city-update.js @@ -29,7 +29,6 @@ define([ options; $.each(region, function (index, value) { - console.log(value+' - '+cityInput); if ( value == cityInput.toUpperCase()) { options = ''; } else {