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