Skip to content

Commit fff50e4

Browse files
pradeep.rauthanpradeep.rauthan
authored andcommitted
MC-42623:Countries list is incorrect when using different countries per website and Global Customer Account settings
1 parent f98865f commit fff50e4

File tree

7 files changed

+134
-17
lines changed

7 files changed

+134
-17
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\Framework\App\ObjectManager;
2323
use Magento\Store\Model\ScopeInterface;
2424
use Magento\Store\Model\StoreManagerInterface;
25+
use Magento\Framework\App\Request\Http;
26+
use Magento\Customer\Api\CustomerRepositoryInterface;
2527

2628
/**
2729
* Return allowed countries for specified website
@@ -54,12 +56,12 @@ class CountryWithWebsites extends Table
5456
private $shareConfig;
5557

5658
/**
57-
* @var \Magento\Framework\App\Request\Http
59+
* @var Http
5860
*/
5961
private $request;
6062

6163
/**
62-
* @var \Magento\Customer\Api\CustomerRepositoryInterface
64+
* @var CustomerRepositoryInterface
6365
*/
6466
private $customerRepository;
6567

@@ -70,8 +72,8 @@ class CountryWithWebsites extends Table
7072
* @param AllowedCountries $allowedCountriesReader
7173
* @param StoreManagerInterface $storeManager
7274
* @param Share $shareConfig
73-
* @param \Magento\Framework\App\Request\Http|null $request
74-
* @param \Magento\Customer\Api\CustomerRepositoryInterface|null $customerRepository
75+
* @param Http|null $request
76+
* @param CustomerRepositoryInterface|null $customerRepository
7577
*/
7678
public function __construct(
7779
OptionCollectionFactory $attrOptionCollectionFactory,
@@ -80,17 +82,17 @@ public function __construct(
8082
AllowedCountries $allowedCountriesReader,
8183
StoreManagerInterface $storeManager,
8284
CustomerShareConfig $shareConfig,
83-
?\Magento\Framework\App\Request\Http $request = null,
84-
?\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository = null
85+
?Http $request = null,
86+
?CustomerRepositoryInterface $customerRepository = null
8587
) {
8688
$this->countriesFactory = $countriesFactory;
8789
$this->allowedCountriesReader = $allowedCountriesReader;
8890
$this->storeManager = $storeManager;
8991
$this->shareConfig = $shareConfig;
9092
$this->request = $request
91-
?? ObjectManager::getInstance()->get(\Magento\Framework\App\Request\Http::class);
93+
?? ObjectManager::getInstance()->get(Http::class);
9294
$this->customerRepository = $customerRepository
93-
?? ObjectManager::getInstance()->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
95+
?? ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
9496
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
9597
}
9698

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Sales\ViewModel\Customer\Address;
10+
11+
use Magento\Customer\Model\ResourceModel\Address\Collection;
12+
use Magento\Directory\Model\AllowedCountries;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
16+
/**
17+
* Customer's addresses filter as per allowed country filter for corresponding store
18+
*/
19+
class AddressAttributeFilter extends \Magento\Eav\Model\Entity\Collection\VersionControl\AbstractCollection implements ArgumentInterface
20+
{
21+
/**
22+
* @var AllowedCountries
23+
*/
24+
private $allowedCountryReader;
25+
26+
/**
27+
* @var Collection
28+
*/
29+
private $collection;
30+
31+
/**
32+
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
33+
* @param \Psr\Log\LoggerInterface $logger
34+
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
35+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
36+
* @param \Magento\Eav\Model\Config $eavConfig
37+
* @param \Magento\Framework\App\ResourceConnection $resource
38+
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
39+
* @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper
40+
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
41+
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
42+
* @param AllowedCountries $allowedCountryReader
43+
* @param Collection $collection
44+
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
45+
*
46+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
47+
*/
48+
public function __construct(
49+
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
50+
\Psr\Log\LoggerInterface $logger,
51+
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
52+
\Magento\Framework\Event\ManagerInterface $eventManager,
53+
\Magento\Eav\Model\Config $eavConfig,
54+
\Magento\Framework\App\ResourceConnection $resource,
55+
\Magento\Eav\Model\EntityFactory $eavEntityFactory,
56+
\Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
57+
\Magento\Framework\Validator\UniversalFactory $universalFactory,
58+
\Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
59+
AllowedCountries $allowedCountryReader,
60+
Collection $collection,
61+
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
62+
) {
63+
$this->allowedCountryReader = $allowedCountryReader;
64+
$this->collection = $collection;
65+
66+
parent::__construct(
67+
$entityFactory,
68+
$logger,
69+
$fetchStrategy,
70+
$eventManager,
71+
$eavConfig,
72+
$resource,
73+
$eavEntityFactory,
74+
$resourceHelper,
75+
$universalFactory,
76+
$entitySnapshot,
77+
$connection
78+
);
79+
}
80+
81+
/**
82+
* Resource initialization
83+
*
84+
* @return void
85+
*/
86+
protected function _construct()
87+
{
88+
$this->_init(\Magento\Customer\Model\Address::class, \Magento\Customer\Model\ResourceModel\Address::class);
89+
}
90+
91+
/**
92+
* Set allowed country filter for customer's addresses
93+
*
94+
* @return $this|Object
95+
* @throws \Magento\Framework\Exception\LocalizedException
96+
*/
97+
public function setScopeFilter($storeId)
98+
{
99+
if ($storeId) {
100+
$allowedCountries = $this->allowedCountryReader->getAllowedCountries(
101+
ScopeInterface::SCOPE_STORE,
102+
$storeId
103+
);
104+
$this->collection->addAttributeToFilter('country_id', ['in' => $allowedCountries]);
105+
}
106+
107+
return $this->collection;
108+
}
109+
}

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_index.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
4949
<arguments>
5050
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
51-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
51+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
52+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
5253
</arguments>
5354
</block>
5455
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
5556
<arguments>
5657
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
57-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
58+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
59+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
5860
</arguments>
5961
</block>
6062
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_billing_address.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
1212
<arguments>
1313
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
14-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
14+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
1515
<argument name="billingAddressDataProvider" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\Billing\AddressDataProvider</argument>
16+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
1617
</arguments>
1718
</block>
1819
</referenceContainer>

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_data.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
2424
<arguments>
2525
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
26-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
26+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
27+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
2728
</arguments>
2829
</block>
2930
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
3031
<arguments>
3132
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
32-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
33+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
34+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
3335
</arguments>
3436
</block>
3537
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_shipping_address.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
1212
<arguments>
1313
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
14-
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\StoreAddressCollection</argument>
14+
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
15+
<argument name="view_model" xsi:type="object">Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter</argument>
1516
</arguments>
1617
</block>
1718
</referenceContainer>

app/code/Magento/Sales/view/adminhtml/templates/order/create/form/address.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*/
1111

1212
/**
13-
* @var \Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection
13+
* @var $viewModel \Magento\Sales\ViewModel\Customer\Address\AddressAttributeFilter
1414
*/
15-
$addressCollection = $block->getData('customerAddressCollection');
15+
$viewModel = $block->getViewModel();
1616

1717
$addressArray = [];
1818
if ($block->getCustomerId()):
19-
$addressArray = $addressCollection->setCustomerFilter([$block->getCustomerId()])->toArray();
19+
$addressArray = $viewModel->setScopeFilter($block->getStoreId())->setCustomerFilter([$block->getCustomerId()])->toArray();
2020
endif;
2121

2222
/**

0 commit comments

Comments
 (0)