Skip to content

Commit 0899f8a

Browse files
author
Stanislav Idolov
authored
ENGCOM-982: [Forwardport] resolved default country selection issue while creating new customer … #5 #14204
2 parents 2b394fd + 95a5e31 commit 0899f8a

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

7074
/**
7175
* Initialize dependencies.
@@ -83,6 +87,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8387
* @param array $countriesWithNotRequiredStates
8488
* @param mixed $connection
8589
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
90+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
8691
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8792
*/
8893
public function __construct(
@@ -98,7 +103,8 @@ public function __construct(
98103
\Magento\Framework\App\Helper\AbstractHelper $helperData,
99104
array $countriesWithNotRequiredStates = [],
100105
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
101-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
106+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
107+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
102108
) {
103109
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
104110
$this->_scopeConfig = $scopeConfig;
@@ -108,6 +114,9 @@ public function __construct(
108114
$this->_arrayUtils = $arrayUtils;
109115
$this->helperData = $helperData;
110116
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
117+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
118+
\Magento\Store\Model\StoreManagerInterface::class
119+
);
111120
}
112121

113122
/**
@@ -273,6 +282,7 @@ public function toOptionArray($emptyLabel = ' ')
273282
$sort = [$name => $foregroundCountry] + $sort;
274283
}
275284
$isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
285+
276286
$options = [];
277287
foreach ($sort as $label => $value) {
278288
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -291,9 +301,36 @@ public function toOptionArray($emptyLabel = ' ')
291301
array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
292302
}
293303

304+
$this->addDefaultCountryToOptions($options);
305+
294306
return $options;
295307
}
296308

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

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: 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)