Skip to content

Commit 63107ab

Browse files
ENGCOM-699: resolved default country selection issue while creating new customer … #13024
- Merge Pull Request #13024 from pradeep-wagento/magento2:feature-country-selection-3483 - Merged commits: 1. 2fb34ef 2. c0ef780 3. 8266e03
2 parents 143ca44 + 8266e03 commit 63107ab

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

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

Lines changed: 38 additions & 1 deletion
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+
private $storeManager;
7175

7276
/**
7377
* Initialize dependencies.
@@ -85,6 +89,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8589
* @param array $countriesWithNotRequiredStates
8690
* @param mixed $connection
8791
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
92+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
8893
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8994
*/
9095
public function __construct(
@@ -100,7 +105,8 @@ public function __construct(
100105
\Magento\Framework\App\Helper\AbstractHelper $helperData,
101106
array $countriesWithNotRequiredStates = [],
102107
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
103-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
108+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
109+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
104110
) {
105111
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
106112
$this->_scopeConfig = $scopeConfig;
@@ -110,6 +116,9 @@ public function __construct(
110116
$this->_arrayUtils = $arrayUtils;
111117
$this->helperData = $helperData;
112118
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
119+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
120+
\Magento\Store\Model\StoreManagerInterface::class
121+
);
113122
}
114123

115124
/**
@@ -275,6 +284,7 @@ public function toOptionArray($emptyLabel = ' ')
275284
$sort = [$name => $foregroundCountry] + $sort;
276285
}
277286
$isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
287+
278288
$options = [];
279289
foreach ($sort as $label => $value) {
280290
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -293,9 +303,36 @@ public function toOptionArray($emptyLabel = ' ')
293303
array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
294304
}
295305

306+
$this->addDefaultCountryToOptions($options);
307+
296308
return $options;
297309
}
298310

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+
299336
/**
300337
* Set foreground countries array
301338
*

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: 12 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, defaultValue;
3232

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

4747
this.setOptions(result);
4848
this.reset();
49+
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+
}
59+
}
4960
}
5061
});
5162
});

0 commit comments

Comments
 (0)