CountryRegion.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Directory\Controller\Adminhtml\Json;
  8. class CountryRegion extends \Magento\Backend\App\Action
  9. {
  10. /**
  11. * Return JSON-encoded array of country regions
  12. *
  13. * @return string
  14. */
  15. public function execute()
  16. {
  17. $arrRes = [];
  18. $countryId = $this->getRequest()->getParam('parent');
  19. if (!empty($countryId)) {
  20. $arrRegions = $this->_objectManager->create(
  21. \Magento\Directory\Model\ResourceModel\Region\Collection::class
  22. )->addCountryFilter(
  23. $countryId
  24. )->load()->toOptionArray();
  25. if (!empty($arrRegions)) {
  26. foreach ($arrRegions as $region) {
  27. $arrRes[] = $region;
  28. }
  29. }
  30. }
  31. $this->getResponse()->representJson(
  32. $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($arrRes)
  33. );
  34. }
  35. }