Skip to content

Commit 8266e03

Browse files
author
Stanislav Idolov
committed
1 parent c0ef780 commit 8266e03

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
@@ -71,7 +71,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
7171
/**
7272
* @var \Magento\Store\Model\StoreManagerInterface
7373
*/
74-
protected $storeManager;
74+
private $storeManager;
7575

7676
/**
7777
* Initialize dependencies.
@@ -86,10 +86,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8686
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
8787
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
8888
* @param \Magento\Framework\App\Helper\AbstractHelper $helperData
89-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
9089
* @param array $countriesWithNotRequiredStates
9190
* @param mixed $connection
9291
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
92+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
9393
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9494
*/
9595
public function __construct(
@@ -103,10 +103,10 @@ public function __construct(
103103
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
104104
\Magento\Framework\Locale\ResolverInterface $localeResolver,
105105
\Magento\Framework\App\Helper\AbstractHelper $helperData,
106-
\Magento\Store\Model\StoreManagerInterface $storeManager,
107106
array $countriesWithNotRequiredStates = [],
108107
\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
110110
) {
111111
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
112112
$this->_scopeConfig = $scopeConfig;
@@ -115,8 +115,10 @@ public function __construct(
115115
$this->_countryFactory = $countryFactory;
116116
$this->_arrayUtils = $arrayUtils;
117117
$this->helperData = $helperData;
118-
$this->storeManager = $storeManager;
119118
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
119+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
120+
\Magento\Store\Model\StoreManagerInterface::class
121+
);
120122
}
121123

122124
/**
@@ -282,15 +284,7 @@ public function toOptionArray($emptyLabel = ' ')
282284
$sort = [$name => $foregroundCountry] + $sort;
283285
}
284286
$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+
294288
$options = [];
295289
foreach ($sort as $label => $value) {
296290
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -303,18 +297,42 @@ public function toOptionArray($emptyLabel = ' ')
303297
if ($this->helperData->isZipCodeOptional($value)) {
304298
$option['is_zipcode_optional'] = true;
305299
}
306-
if (isset($defaultCountry[$value])) {
307-
$option['is_default'] = $defaultCountry[$value];
308-
}
309300
$options[] = $option;
310301
}
311302
if ($emptyLabel !== false && count($options) > 0) {
312303
array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
313304
}
314305

306+
$this->addDefaultCountryToOptions($options);
307+
315308
return $options;
316309
}
317310

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

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)