24 private $countryOptions;
29 private $regionOptions;
34 private $regionCollectionFactory;
39 private $countryCollectionFactory;
44 private $storeManager;
49 private $directoryHelper;
63 DirectoryHelper $directoryHelper,
66 $this->countryCollectionFactory = $countryCollection;
67 $this->regionCollectionFactory = $regionCollection;
68 $this->directoryHelper = $directoryHelper;
80 if (!isset($jsLayout[
'components'][
'checkoutProvider'][
'dictionaries'])) {
81 $jsLayout[
'components'][
'checkoutProvider'][
'dictionaries'] = [
82 'country_id' => $this->getCountryOptions(),
83 'region_id' => $this->getRegionOptions(),
95 private function getCountryOptions()
97 if (!isset($this->countryOptions)) {
98 $this->countryOptions = $this->countryCollectionFactory->create()->loadByStore(
99 $this->storeManager->getStore()->getId()
101 $this->countryOptions = $this->orderCountryOptions($this->countryOptions);
104 return $this->countryOptions;
112 private function getRegionOptions()
114 if (!isset($this->regionOptions)) {
115 $this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
116 $this->storeManager->getStore()->getId()
120 return $this->regionOptions;
129 private function orderCountryOptions(array $countryOptions)
131 $topCountryCodes = $this->directoryHelper->getTopCountryCodes();
132 if (empty($topCountryCodes)) {
133 return $countryOptions;
138 'value' =>
'delimiter',
139 'label' =>
'──────────',
142 foreach ($countryOptions as $countryOption) {
143 if (empty($countryOption[
'value']) || in_array($countryOption[
'value'], $topCountryCodes)) {
144 $headOptions[] = $countryOption;
146 $tailOptions[] = $countryOption;
149 return array_merge($headOptions, $tailOptions);
__construct(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection, StoreResolverInterface $storeResolver, DirectoryHelper $directoryHelper, StoreManagerInterface $storeManager=null)