Skip to content

Commit 255b682

Browse files
pradeep-wagentorostyslav-hymon
authored andcommitted
resolved default country selection issue while creating new customer from backend
1 parent 447a24d commit 255b682

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
@@ -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+
protected $storeManager;
6973

7074
/**
7175
* Initialize dependencies.
@@ -80,6 +84,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8084
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
8185
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
8286
* @param \Magento\Framework\App\Helper\AbstractHelper $helperData
87+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
8388
* @param array $countriesWithNotRequiredStates
8489
* @param mixed $connection
8590
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
@@ -96,6 +101,7 @@ public function __construct(
96101
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
97102
\Magento\Framework\Locale\ResolverInterface $localeResolver,
98103
\Magento\Framework\App\Helper\AbstractHelper $helperData,
104+
\Magento\Store\Model\StoreManagerInterface $storeManager,
99105
array $countriesWithNotRequiredStates = [],
100106
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
101107
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
@@ -107,6 +113,7 @@ public function __construct(
107113
$this->_countryFactory = $countryFactory;
108114
$this->_arrayUtils = $arrayUtils;
109115
$this->helperData = $helperData;
116+
$this->storeManager = $storeManager;
110117
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
111118
}
112119

@@ -273,6 +280,15 @@ public function toOptionArray($emptyLabel = ' ')
273280
$sort = [$name => $foregroundCountry] + $sort;
274281
}
275282
$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+
}
276292
$options = [];
277293
foreach ($sort as $label => $value) {
278294
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -285,6 +301,9 @@ public function toOptionArray($emptyLabel = ' ')
285301
if ($this->helperData->isZipCodeOptional($value)) {
286302
$option['is_zipcode_optional'] = true;
287303
}
304+
if (isset($defaultCountry[$value])) {
305+
$option['is_default'] = $defaultCountry[$value];
306+
}
288307
$options[] = $option;
289308
}
290309
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
@@ -6,6 +6,8 @@
66

77
namespace Magento\Directory\Test\Unit\Model\ResourceModel\Country;
88

9+
use Magento\Store\Api\Data\WebsiteInterface;
10+
911
/**
1012
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1113
*/
@@ -21,6 +23,11 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
2123
*/
2224
protected $scopeConfigMock;
2325

26+
/**
27+
* @var \Magento\Store\Model\StoreManagerInterface
28+
*/
29+
protected $storeManagerMock;
30+
2431
protected function setUp()
2532
{
2633
$connection = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
@@ -51,6 +58,7 @@ protected function setUp()
5158
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
5259
$countryFactory = $this->createMock(\Magento\Directory\Model\ResourceModel\CountryFactory::class);
5360
$helperDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
61+
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
5462
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
5563
$arguments = [
5664
'logger' => $logger,
@@ -61,7 +69,8 @@ protected function setUp()
6169
'scopeConfig' => $this->scopeConfigMock,
6270
'countryFactory' => $countryFactory,
6371
'resource' => $resource,
64-
'helperData' => $helperDataMock
72+
'helperData' => $helperDataMock,
73+
'storeManager' => $this->storeManagerMock
6574
];
6675
$this->_model = $objectManager
6776
->getObject(\Magento\Directory\Model\ResourceModel\Country\Collection::class, $arguments);
@@ -76,6 +85,14 @@ protected function setUp()
7685
*/
7786
public function testToOptionArray($optionsArray, $emptyLabel, $foregroundCountries, $expectedResults)
7887
{
88+
$website1 = $this->createMock(WebsiteInterface::class);
89+
$website1->expects($this->atLeastOnce())
90+
->method('getId')
91+
->willReturn(1);
92+
$this->storeManagerMock->expects($this->once())
93+
->method('getWebsites')
94+
->willReturn([$website1]);
95+
7996
foreach ($optionsArray as $itemData) {
8097
$this->_model->addItem(new \Magento\Framework\DataObject($itemData));
8198
}

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)