|
19 | 19 | *
|
20 | 20 | * @category Mage
|
21 | 21 | * @package Mage_Directory
|
| 22 | + * @phpstan-type Option array{label: string, value: non-falsy-string} |
22 | 23 | *
|
23 | 24 | * @method int getRegionId()
|
24 | 25 | */
|
@@ -78,7 +79,7 @@ public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id
|
78 | 79 | ->setTitle(Mage::helper('directory')->__($title))
|
79 | 80 | ->setClass('validate-select')
|
80 | 81 | ->setValue($defValue)
|
81 |
| - ->setOptions($options) |
| 82 | + ->setOptions($this->sortCountryOptions($options)) |
82 | 83 | ->getHtml();
|
83 | 84 |
|
84 | 85 | Varien_Profiler::stop('TEST: ' . __METHOD__);
|
@@ -172,4 +173,29 @@ public function getRegionsJs()
|
172 | 173 | Varien_Profiler::stop('TEST: ' . __METHOD__);
|
173 | 174 | return $regionsJs;
|
174 | 175 | }
|
| 176 | + |
| 177 | + /** |
| 178 | + * @template T of Option[] |
| 179 | + * @param T $countryOptions |
| 180 | + * @return array{0: array{label: string, value: Option[]}, 1: array{label: string, value: Option[]}}|T |
| 181 | + */ |
| 182 | + private function sortCountryOptions(array $countryOptions): array |
| 183 | + { |
| 184 | + $topCountryCodes = $this->helper('directory')->getTopCountryCodes(); |
| 185 | + $headOptions = $tailOptions = []; |
| 186 | + |
| 187 | + foreach ($countryOptions as $countryOption) { |
| 188 | + if (in_array($countryOption['value'], $topCountryCodes)) { |
| 189 | + $headOptions[] = $countryOption; |
| 190 | + } else { |
| 191 | + $tailOptions[] = $countryOption; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + if (empty($headOptions)) { |
| 196 | + return $countryOptions; |
| 197 | + } |
| 198 | + |
| 199 | + return [['label' => $this->__('Popular'), 'value' => $headOptions], ['label' => $this->__('Others'), 'value' => $tailOptions]]; |
| 200 | + } |
175 | 201 | }
|
0 commit comments