@@ -69,7 +69,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
69
69
/**
70
70
* @var \Magento\Store\Model\StoreManagerInterface
71
71
*/
72
- protected $ storeManager ;
72
+ private $ storeManager ;
73
73
74
74
/**
75
75
* Initialize dependencies.
@@ -84,10 +84,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
84
84
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
85
85
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
86
86
* @param \Magento\Framework\App\Helper\AbstractHelper $helperData
87
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
88
87
* @param array $countriesWithNotRequiredStates
89
88
* @param mixed $connection
90
89
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
90
+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
91
91
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
92
92
*/
93
93
public function __construct (
@@ -101,10 +101,10 @@ public function __construct(
101
101
\Magento \Framework \Stdlib \ArrayUtils $ arrayUtils ,
102
102
\Magento \Framework \Locale \ResolverInterface $ localeResolver ,
103
103
\Magento \Framework \App \Helper \AbstractHelper $ helperData ,
104
- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
105
104
array $ countriesWithNotRequiredStates = [],
106
105
\Magento \Framework \DB \Adapter \AdapterInterface $ connection = null ,
107
- \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null
106
+ \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null ,
107
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null
108
108
) {
109
109
parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
110
110
$ this ->_scopeConfig = $ scopeConfig ;
@@ -113,8 +113,10 @@ public function __construct(
113
113
$ this ->_countryFactory = $ countryFactory ;
114
114
$ this ->_arrayUtils = $ arrayUtils ;
115
115
$ this ->helperData = $ helperData ;
116
- $ this ->storeManager = $ storeManager ;
117
116
$ this ->countriesWithNotRequiredStates = $ countriesWithNotRequiredStates ;
117
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (
118
+ \Magento \Store \Model \StoreManagerInterface::class
119
+ );
118
120
}
119
121
120
122
/**
@@ -280,15 +282,7 @@ public function toOptionArray($emptyLabel = ' ')
280
282
$ sort = [$ name => $ foregroundCountry ] + $ sort ;
281
283
}
282
284
$ isRegionVisible = (bool )$ this ->helperData ->isShowNonRequiredState ();
283
- $ defaultCountry = [];
284
- foreach ($ this ->storeManager ->getWebsites () as $ website ) {
285
- $ defaultCountryConfig = $ this ->_scopeConfig ->getValue (
286
- \Magento \Directory \Helper \Data::XML_PATH_DEFAULT_COUNTRY ,
287
- ScopeInterface::SCOPE_WEBSITES ,
288
- $ website
289
- );
290
- $ defaultCountry [$ defaultCountryConfig ][] = $ website ->getId ();
291
- }
285
+
292
286
$ options = [];
293
287
foreach ($ sort as $ label => $ value ) {
294
288
$ options = $ this ->addForegroundCountriesToOptionArray ($ emptyLabel , $ options );
@@ -301,18 +295,42 @@ public function toOptionArray($emptyLabel = ' ')
301
295
if ($ this ->helperData ->isZipCodeOptional ($ value )) {
302
296
$ option ['is_zipcode_optional ' ] = true ;
303
297
}
304
- if (isset ($ defaultCountry [$ value ])) {
305
- $ option ['is_default ' ] = $ defaultCountry [$ value ];
306
- }
307
298
$ options [] = $ option ;
308
299
}
309
300
if ($ emptyLabel !== false && count ($ options ) > 0 ) {
310
301
array_unshift ($ options , ['value ' => '' , 'label ' => $ emptyLabel ]);
311
302
}
312
303
304
+ $ this ->addDefaultCountryToOptions ($ options );
305
+
313
306
return $ options ;
314
307
}
315
308
309
+ /**
310
+ * Adds default country to options
311
+ *
312
+ * @param array $options
313
+ * @return void
314
+ */
315
+ private function addDefaultCountryToOptions (array &$ options )
316
+ {
317
+ $ defaultCountry = [];
318
+ foreach ($ this ->storeManager ->getWebsites () as $ website ) {
319
+ $ defaultCountryConfig = $ this ->_scopeConfig ->getValue (
320
+ \Magento \Directory \Helper \Data::XML_PATH_DEFAULT_COUNTRY ,
321
+ ScopeInterface::SCOPE_WEBSITES ,
322
+ $ website
323
+ );
324
+ $ defaultCountry [$ defaultCountryConfig ][] = $ website ->getId ();
325
+ }
326
+
327
+ foreach ($ options as $ key => $ option ) {
328
+ if (isset ($ defaultCountry [$ option ['value ' ]])) {
329
+ $ options [$ key ]['is_default ' ] = $ defaultCountry [$ option ['value ' ]];
330
+ }
331
+ }
332
+ }
333
+
316
334
/**
317
335
* Set foreground countries array
318
336
*
0 commit comments