Skip to content

Commit 7c31337

Browse files
author
Sergey Kovalenko
committed
MAGETWO-56941: CLONE - CLONE - [Github] Allowed countries for customer address in admin using storeview configuration #2946
1 parent f24bd27 commit 7c31337

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

app/code/Magento/Customer/Model/CountryHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function getCountriesFromConfig($scope, $filter)
117117
/**
118118
* @param $filter
119119
* @param string $scope
120-
* @return $this
120+
* @return AbstractDb
121121
*/
122122
public function loadByScope($filter, $scope = ScopeInterface::SCOPE_STORE, AbstractDb $collection)
123123
{

app/code/Magento/Customer/Model/Customer/DataProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Customer\Model\Attribute;
1313
use Magento\Customer\Model\FileProcessor;
1414
use Magento\Customer\Model\FileProcessorFactory;
15-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryByWebsite;
15+
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
1616
use Magento\Eav\Api\Data\AttributeInterface;
1717
use Magento\Eav\Model\Config;
1818
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
@@ -331,11 +331,11 @@ protected function getAttributesMeta(Type $entityType)
331331

332332
/**
333333
* @deprecated
334-
* @return CountryByWebsite
334+
* @return CountryWithWebsites
335335
*/
336336
private function getCountryByWebsiteSource()
337337
{
338-
return ObjectManager::getInstance()->get(CountryByWebsite::class);
338+
return ObjectManager::getInstance()->get(CountryWithWebsites::class);
339339
}
340340

341341
/**

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/Country.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
use Magento\Framework\App\ObjectManager;
1616
use Magento\Store\Model\StoreManagerInterface;
1717

18+
/**
19+
* Class Country.
20+
* @package Magento\Customer\Model\ResourceModel\Address\Attribute\Source
21+
*/
1822
class Country extends \Magento\Eav\Model\Entity\Attribute\Source\Table
1923
{
2024
/**
@@ -45,35 +49,12 @@ public function getAllOptions()
4549
{
4650
if (!$this->_options) {
4751
$this->_options = $this->_createCountriesCollection()->loadByStore(
48-
$this->resolveStoreId()
52+
$this->getStoreManager()->getStore()->getId()
4953
)->toOptionArray();
5054
}
5155
return $this->_options;
5256
}
5357

54-
/**
55-
* @deprecated
56-
* @return \Magento\Backend\Model\Session\Quote
57-
*/
58-
private function getBackendSession()
59-
{
60-
return ObjectManager::getInstance()->get(\Magento\Backend\Model\Session\Quote::class);
61-
}
62-
63-
/**
64-
* Retrieve store id in view of backend quote.
65-
* @return int
66-
*/
67-
private function resolveStoreId()
68-
{
69-
$backendSession = $this->getBackendSession();
70-
if ($backendSession->getQuoteId() && $backendSession->getQuote()->hasStoreId()) {
71-
return $backendSession->getQuote()->getStoreId();
72-
}
73-
74-
return $this->getStoreManager()->getStore()->getId();
75-
}
76-
7758
/**
7859
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
7960
*/

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryByWebsite.php renamed to app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Magento\Store\Model\ScopeInterface;
1919
use Magento\Store\Model\StoreManagerInterface;
2020

21-
class CountryByWebsite extends \Magento\Eav\Model\Entity\Attribute\Source\Table
21+
class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Table
2222
{
2323
/**
2424
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory

app/code/Magento/Customer/Test/Unit/Model/Customer/DataProviderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Customer\Api\CustomerMetadataInterface;
99
use Magento\Customer\Model\Config\Share;
10-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryByWebsite;
10+
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
1111
use Magento\Eav\Model\Config;
1212
use Magento\Eav\Model\Entity\Type;
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -401,7 +401,7 @@ function ($origName) {
401401

402402
private function getCountryAttrMock()
403403
{
404-
$countryByWebsiteMock = $this->getMockBuilder(CountryByWebsite::class)
404+
$countryByWebsiteMock = $this->getMockBuilder(CountryWithWebsites::class)
405405
->disableOriginalConstructor()
406406
->getMock();
407407
$countryByWebsiteMock->expects($this->any())
@@ -414,7 +414,7 @@ private function getCountryAttrMock()
414414
$objectManagerMock->expects($this->any())
415415
->method('get')
416416
->willReturnMap([
417-
[CountryByWebsite::class, $countryByWebsiteMock],
417+
[CountryWithWebsites::class, $countryByWebsiteMock],
418418
[Share::class, $shareMock],
419419
]);
420420
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Magento\Customer\Test\Unit\Model\ResourceModel\Address\Attribute\Source;
88

9-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryByWebsite;
9+
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
1010
use Magento\Customer\Model\CountryHandler;
1111
use Magento\Framework\Data\Collection\AbstractDb;
1212
use Magento\Store\Api\Data\WebsiteInterface;
1313
use Magento\Store\Model\StoreManagerInterface;
1414

15-
class CountryByWebsiteTest extends \PHPUnit_Framework_TestCase
15+
class CountryWithWebsitesTest extends \PHPUnit_Framework_TestCase
1616
{
1717
/**
1818
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory | \PHPUnit_Framework_MockObject_MockObject
@@ -30,7 +30,7 @@ class CountryByWebsiteTest extends \PHPUnit_Framework_TestCase
3030
private $storeManagerMock;
3131

3232
/**
33-
* @var CountryByWebsite
33+
* @var CountryWithWebsites
3434
*/
3535
private $countryByWebsite;
3636

@@ -53,7 +53,7 @@ public function setUp()
5353
->disableOriginalConstructor()
5454
->getMock();
5555
$this->storeManagerMock = $this->getMock(StoreManagerInterface::class);
56-
$this->countryByWebsite = new CountryByWebsite(
56+
$this->countryByWebsite = new CountryWithWebsites(
5757
$eavCollectionFactoryMock,
5858
$optionsFactoryMock,
5959
$this->countriesFactoryMock,

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Address.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
77

8+
use Magento\Backend\Model\Session\Quote;
9+
use Magento\Customer\Model\CountryHandler;
10+
use Magento\Framework\App\ObjectManager;
811
use Magento\Framework\Data\Form\Element\AbstractElement;
912
use Magento\Framework\Pricing\PriceCurrencyInterface;
13+
use Magento\Store\Model\ScopeInterface;
1014

1115
/**
1216
* Order create address form
@@ -250,6 +254,7 @@ protected function _prepareForm()
250254
$this->_form->setValues($this->getFormValues());
251255

252256
if ($this->_form->getElement('country_id')->getValue()) {
257+
$this->processCountryOptions($this->_form->getElement('country_id'));
253258
$countryId = $this->_form->getElement('country_id')->getValue();
254259
$this->_form->getElement('country_id')->setValue(null);
255260
foreach ($this->_form->getElement('country_id')->getValues() as $country) {
@@ -279,6 +284,47 @@ protected function _prepareForm()
279284
return $this;
280285
}
281286

287+
/**
288+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $countryElement
289+
* @return void
290+
*/
291+
private function processCountryOptions(\Magento\Framework\Data\Form\Element\AbstractElement $countryElement)
292+
{
293+
$storeId = $this->getBackendQuoteSession()->getStoreId();
294+
$options = $this->getCountryHandler()
295+
->loadByScope($storeId, ScopeInterface::SCOPE_STORE, $this->getCountriesCollection())
296+
->toOptionArray();
297+
298+
$countryElement->setValues($options);
299+
}
300+
301+
/**
302+
* @deprecated
303+
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
304+
*/
305+
private function getCountriesCollection()
306+
{
307+
return ObjectManager::getInstance()->get(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
308+
}
309+
310+
/**
311+
* @deprecated
312+
* @return CountryHandler
313+
*/
314+
private function getCountryHandler()
315+
{
316+
return ObjectManager::getInstance()->get(CountryHandler::class);
317+
}
318+
319+
/**
320+
* @deprecated
321+
* @return Quote
322+
*/
323+
private function getBackendQuoteSession()
324+
{
325+
return ObjectManager::getInstance()->get(Quote::class);
326+
}
327+
282328
/**
283329
* Add additional data to form element
284330
*

0 commit comments

Comments
 (0)