Skip to content

Commit e37ae24

Browse files
Stanislav Idolovrostyslav-hymon
authored andcommitted
1 parent 255b682 commit e37ae24

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
6969
/**
7070
* @var \Magento\Store\Model\StoreManagerInterface
7171
*/
72-
protected $storeManager;
72+
private $storeManager;
7373

7474
/**
7575
* Initialize dependencies.
@@ -84,10 +84,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8484
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
8585
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
8686
* @param \Magento\Framework\App\Helper\AbstractHelper $helperData
87-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8887
* @param array $countriesWithNotRequiredStates
8988
* @param mixed $connection
9089
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
90+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
9191
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9292
*/
9393
public function __construct(
@@ -101,10 +101,10 @@ public function __construct(
101101
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
102102
\Magento\Framework\Locale\ResolverInterface $localeResolver,
103103
\Magento\Framework\App\Helper\AbstractHelper $helperData,
104-
\Magento\Store\Model\StoreManagerInterface $storeManager,
105104
array $countriesWithNotRequiredStates = [],
106105
\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
108108
) {
109109
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
110110
$this->_scopeConfig = $scopeConfig;
@@ -113,8 +113,10 @@ public function __construct(
113113
$this->_countryFactory = $countryFactory;
114114
$this->_arrayUtils = $arrayUtils;
115115
$this->helperData = $helperData;
116-
$this->storeManager = $storeManager;
117116
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
117+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
118+
\Magento\Store\Model\StoreManagerInterface::class
119+
);
118120
}
119121

120122
/**
@@ -280,15 +282,7 @@ public function toOptionArray($emptyLabel = ' ')
280282
$sort = [$name => $foregroundCountry] + $sort;
281283
}
282284
$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+
292286
$options = [];
293287
foreach ($sort as $label => $value) {
294288
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -301,18 +295,42 @@ public function toOptionArray($emptyLabel = ' ')
301295
if ($this->helperData->isZipCodeOptional($value)) {
302296
$option['is_zipcode_optional'] = true;
303297
}
304-
if (isset($defaultCountry[$value])) {
305-
$option['is_default'] = $defaultCountry[$value];
306-
}
307298
$options[] = $option;
308299
}
309300
if ($emptyLabel !== false && count($options) > 0) {
310301
array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
311302
}
312303

304+
$this->addDefaultCountryToOptions($options);
305+
313306
return $options;
314307
}
315308

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+
316334
/**
317335
* Set foreground countries array
318336
*

app/code/Magento/Ui/view/base/web/js/form/element/country.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ define([
2828
* @param {String} field
2929
*/
3030
filter: function (value, field) {
31-
var result, defaultCountry;
31+
var result, defaultCountry, defaultValue;
3232

3333
if (!field) { //validate field, if we are on update
3434
field = this.filterBy.field;
@@ -47,17 +47,15 @@ define([
4747
this.setOptions(result);
4848
this.reset();
4949

50-
if(!this.value()){
51-
_.each(result, function (item) {
52-
var defaultValue = item['is_default'];
53-
if (defaultValue) {
54-
if(defaultValue.includes(value)){
55-
defaultCountry = item.value;
56-
return;
57-
}
58-
}
59-
});
60-
this.value(defaultCountry);
50+
if (!this.value()) {
51+
defaultCountry = _.filter(result, function (item) {
52+
return item['is_default'] && item['is_default'].includes(value);
53+
});
54+
55+
if (defaultCountry.length) {
56+
defaultValue = defaultCountry.shift();
57+
this.value(defaultValue.value);
58+
}
6159
}
6260
}
6361
});

0 commit comments

Comments
 (0)