Skip to content

Commit 2fb34ef

Browse files
resolved default country selection issue while creating new customer from backend
1 parent a39e37f commit 2fb34ef

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
6868
* @since 100.1.0
6969
*/
7070
protected $countriesWithNotRequiredStates;
71+
/**
72+
* @var \Magento\Store\Model\StoreManagerInterface
73+
*/
74+
protected $storeManager;
7175

7276
/**
7377
* Initialize dependencies.
@@ -82,6 +86,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8286
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
8387
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
8488
* @param \Magento\Framework\App\Helper\AbstractHelper $helperData
89+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8590
* @param array $countriesWithNotRequiredStates
8691
* @param mixed $connection
8792
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
@@ -98,6 +103,7 @@ public function __construct(
98103
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
99104
\Magento\Framework\Locale\ResolverInterface $localeResolver,
100105
\Magento\Framework\App\Helper\AbstractHelper $helperData,
106+
\Magento\Store\Model\StoreManagerInterface $storeManager,
101107
array $countriesWithNotRequiredStates = [],
102108
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
103109
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
@@ -109,6 +115,7 @@ public function __construct(
109115
$this->_countryFactory = $countryFactory;
110116
$this->_arrayUtils = $arrayUtils;
111117
$this->helperData = $helperData;
118+
$this->storeManager = $storeManager;
112119
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
113120
}
114121

@@ -275,6 +282,15 @@ public function toOptionArray($emptyLabel = ' ')
275282
$sort = [$name => $foregroundCountry] + $sort;
276283
}
277284
$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+
}
278294
$options = [];
279295
foreach ($sort as $label => $value) {
280296
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -287,6 +303,9 @@ public function toOptionArray($emptyLabel = ' ')
287303
if ($this->helperData->isZipCodeOptional($value)) {
288304
$option['is_zipcode_optional'] = true;
289305
}
306+
if (isset($defaultCountry[$value])) {
307+
$option['is_default'] = $defaultCountry[$value];
308+
}
290309
$options[] = $option;
291310
}
292311
if ($emptyLabel !== false && count($options) > 0) {

app/code/Magento/Directory/Test/Unit/Model/ResourceModel/Country/CollectionTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Magento\Directory\Test\Unit\Model\ResourceModel\Country;
1010

11+
use Magento\Store\Api\Data\WebsiteInterface;
12+
1113
/**
1214
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1315
*/
@@ -23,6 +25,11 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
2325
*/
2426
protected $scopeConfigMock;
2527

28+
/**
29+
* @var \Magento\Store\Model\StoreManagerInterface
30+
*/
31+
protected $storeManagerMock;
32+
2633
protected function setUp()
2734
{
2835
$connection = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
@@ -53,6 +60,7 @@ protected function setUp()
5360
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
5461
$countryFactory = $this->createMock(\Magento\Directory\Model\ResourceModel\CountryFactory::class);
5562
$helperDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
63+
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
5664
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
5765
$arguments = [
5866
'logger' => $logger,
@@ -63,7 +71,8 @@ protected function setUp()
6371
'scopeConfig' => $this->scopeConfigMock,
6472
'countryFactory' => $countryFactory,
6573
'resource' => $resource,
66-
'helperData' => $helperDataMock
74+
'helperData' => $helperDataMock,
75+
'storeManager' => $this->storeManagerMock
6776
];
6877
$this->_model = $objectManager
6978
->getObject(\Magento\Directory\Model\ResourceModel\Country\Collection::class, $arguments);
@@ -78,6 +87,14 @@ protected function setUp()
7887
*/
7988
public function testToOptionArray($optionsArray, $emptyLabel, $foregroundCountries, $expectedResults)
8089
{
90+
$website1 = $this->createMock(WebsiteInterface::class);
91+
$website1->expects($this->atLeastOnce())
92+
->method('getId')
93+
->willReturn(1);
94+
$this->storeManagerMock->expects($this->once())
95+
->method('getWebsites')
96+
->willReturn([$website1]);
97+
8198
foreach ($optionsArray as $itemData) {
8299
$this->_model->addItem(new \Magento\Framework\DataObject($itemData));
83100
}

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

Lines changed: 14 additions & 1 deletion
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;
31+
var result, defaultCountry;
3232

3333
if (!field) { //validate field, if we are on update
3434
field = this.filterBy.field;
@@ -46,6 +46,19 @@ define([
4646

4747
this.setOptions(result);
4848
this.reset();
49+
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);
61+
}
4962
}
5063
});
5164
});

0 commit comments

Comments
 (0)