@@ -66,6 +66,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
6666 * @since 100.1.0
6767 */
6868 protected $ countriesWithNotRequiredStates ;
69+ /**
70+ * @var \Magento\Store\Model\StoreManagerInterface
71+ */
72+ private $ storeManager ;
6973
7074 /**
7175 * Initialize dependencies.
@@ -83,6 +87,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8387 * @param array $countriesWithNotRequiredStates
8488 * @param mixed $connection
8589 * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
90+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
8691 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
8792 */
8893 public function __construct (
@@ -98,7 +103,8 @@ public function __construct(
98103 \Magento \Framework \App \Helper \AbstractHelper $ helperData ,
99104 array $ countriesWithNotRequiredStates = [],
100105 \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
102108 ) {
103109 parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
104110 $ this ->_scopeConfig = $ scopeConfig ;
@@ -108,6 +114,9 @@ public function __construct(
108114 $ this ->_arrayUtils = $ arrayUtils ;
109115 $ this ->helperData = $ helperData ;
110116 $ this ->countriesWithNotRequiredStates = $ countriesWithNotRequiredStates ;
117+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (
118+ \Magento \Store \Model \StoreManagerInterface::class
119+ );
111120 }
112121
113122 /**
@@ -273,6 +282,7 @@ public function toOptionArray($emptyLabel = ' ')
273282 $ sort = [$ name => $ foregroundCountry ] + $ sort ;
274283 }
275284 $ isRegionVisible = (bool )$ this ->helperData ->isShowNonRequiredState ();
285+
276286 $ options = [];
277287 foreach ($ sort as $ label => $ value ) {
278288 $ options = $ this ->addForegroundCountriesToOptionArray ($ emptyLabel , $ options );
@@ -291,9 +301,36 @@ public function toOptionArray($emptyLabel = ' ')
291301 array_unshift ($ options , ['value ' => '' , 'label ' => $ emptyLabel ]);
292302 }
293303
304+ $ this ->addDefaultCountryToOptions ($ options );
305+
294306 return $ options ;
295307 }
296308
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+
297334 /**
298335 * Set foreground countries array
299336 *
0 commit comments