@@ -68,6 +68,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
68
68
* @since 100.1.0
69
69
*/
70
70
protected $ countriesWithNotRequiredStates ;
71
+ /**
72
+ * @var \Magento\Store\Model\StoreManagerInterface
73
+ */
74
+ private $ storeManager ;
71
75
72
76
/**
73
77
* Initialize dependencies.
@@ -85,6 +89,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
85
89
* @param array $countriesWithNotRequiredStates
86
90
* @param mixed $connection
87
91
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
92
+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
88
93
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
89
94
*/
90
95
public function __construct (
@@ -100,7 +105,8 @@ public function __construct(
100
105
\Magento \Framework \App \Helper \AbstractHelper $ helperData ,
101
106
array $ countriesWithNotRequiredStates = [],
102
107
\Magento \Framework \DB \Adapter \AdapterInterface $ connection = null ,
103
- \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null
108
+ \Magento \Framework \Model \ResourceModel \Db \AbstractDb $ resource = null ,
109
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null
104
110
) {
105
111
parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
106
112
$ this ->_scopeConfig = $ scopeConfig ;
@@ -110,6 +116,9 @@ public function __construct(
110
116
$ this ->_arrayUtils = $ arrayUtils ;
111
117
$ this ->helperData = $ helperData ;
112
118
$ this ->countriesWithNotRequiredStates = $ countriesWithNotRequiredStates ;
119
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (
120
+ \Magento \Store \Model \StoreManagerInterface::class
121
+ );
113
122
}
114
123
115
124
/**
@@ -275,6 +284,7 @@ public function toOptionArray($emptyLabel = ' ')
275
284
$ sort = [$ name => $ foregroundCountry ] + $ sort ;
276
285
}
277
286
$ isRegionVisible = (bool )$ this ->helperData ->isShowNonRequiredState ();
287
+
278
288
$ options = [];
279
289
foreach ($ sort as $ label => $ value ) {
280
290
$ options = $ this ->addForegroundCountriesToOptionArray ($ emptyLabel , $ options );
@@ -293,9 +303,36 @@ public function toOptionArray($emptyLabel = ' ')
293
303
array_unshift ($ options , ['value ' => '' , 'label ' => $ emptyLabel ]);
294
304
}
295
305
306
+ $ this ->addDefaultCountryToOptions ($ options );
307
+
296
308
return $ options ;
297
309
}
298
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
+
299
336
/**
300
337
* Set foreground countries array
301
338
*
0 commit comments