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