Skip to content

Commit 6c2962e

Browse files
committed
MAGETWO-98832: Multistore Allowed Countries List Problem
1 parent 60e3a73 commit 6c2962e

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use Magento\Customer\Model\Config\Share as ConfigShare;
1818
use Magento\Customer\Model\Customer as CustomerModel;
1919
use Magento\Customer\Model\Customer\CredentialsValidator;
20+
use Magento\Customer\Model\Data\Address as DataAddress;
2021
use Magento\Customer\Model\Metadata\Validator;
2122
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
23+
use Magento\Directory\Model\AllowedCountries;
2224
use Magento\Eav\Model\Validator\Attribute\Backend;
2325
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2426
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -339,6 +341,11 @@ class AccountManagement implements AccountManagementInterface
339341
*/
340342
private $addressRegistry;
341343

344+
/**
345+
* @var AllowedCountries
346+
*/
347+
private $allowedCountriesReader;
348+
342349
/**
343350
* @param CustomerFactory $customerFactory
344351
* @param ManagerInterface $eventManager
@@ -405,7 +412,8 @@ public function __construct(
405412
SaveHandlerInterface $saveHandler = null,
406413
CollectionFactory $visitorCollectionFactory = null,
407414
SearchCriteriaBuilder $searchCriteriaBuilder = null,
408-
AddressRegistry $addressRegistry = null
415+
AddressRegistry $addressRegistry = null,
416+
AllowedCountries $allowedCountriesReader = null
409417
) {
410418
$this->customerFactory = $customerFactory;
411419
$this->eventManager = $eventManager;
@@ -445,6 +453,8 @@ public function __construct(
445453
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
446454
$this->addressRegistry = $addressRegistry
447455
?: ObjectManager::getInstance()->get(AddressRegistry::class);
456+
$this->allowedCountriesReader = $allowedCountriesReader
457+
?: ObjectManager::getInstance()->get(AllowedCountries::class);
448458
}
449459

450460
/**
@@ -895,6 +905,9 @@ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash
895905
}
896906
try {
897907
foreach ($customerAddresses as $address) {
908+
if (!$this->isAddressAllowedForWebsite($address, (int)$customer->getStoreId())) {
909+
continue;
910+
}
898911
if ($address->getId()) {
899912
$newAddress = clone $address;
900913
$newAddress->setId(null);
@@ -1601,4 +1614,18 @@ private function setIgnoreValidationFlag($customer)
16011614
{
16021615
$customer->setData('ignore_validation_flag', true);
16031616
}
1617+
1618+
/**
1619+
* Check is address allowed for store
1620+
*
1621+
* @param DataAddress $address
1622+
* @param int $storeId
1623+
* @return bool
1624+
*/
1625+
private function isAddressAllowedForWebsite(DataAddress $address, int $storeId): bool
1626+
{
1627+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
1628+
1629+
return in_array($address->getCountryId(), $allowedCountries);
1630+
}
16041631
}

app/code/Magento/Quote/Model/Quote.php

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

88
use Magento\Customer\Api\Data\CustomerInterface;
99
use Magento\Customer\Api\Data\GroupInterface;
10+
use Magento\Directory\Model\AllowedCountries;
1011
use Magento\Framework\Api\AttributeValueFactory;
1112
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1213
use Magento\Framework\Model\AbstractExtensibleModel;
1314
use Magento\Quote\Api\Data\PaymentInterface;
1415
use Magento\Quote\Model\Quote\Address;
1516
use Magento\Quote\Model\Quote\Address\Total as AddressTotal;
1617
use Magento\Sales\Model\Status;
18+
use Magento\Store\Model\ScopeInterface;
1719
use Magento\Framework\App\ObjectManager;
1820

1921
/**
@@ -359,6 +361,11 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
359361
*/
360362
private $orderIncrementIdChecker;
361363

364+
/**
365+
* @var AllowedCountries
366+
*/
367+
private $allowedCountriesReader;
368+
362369
/**
363370
* @param \Magento\Framework\Model\Context $context
364371
* @param \Magento\Framework\Registry $registry
@@ -444,7 +451,8 @@ public function __construct(
444451
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
445452
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
446453
array $data = [],
447-
\Magento\Sales\Model\OrderIncrementIdChecker $orderIncrementIdChecker = null
454+
\Magento\Sales\Model\OrderIncrementIdChecker $orderIncrementIdChecker = null,
455+
AllowedCountries $allowedCountriesReader = null
448456
) {
449457
$this->quoteValidator = $quoteValidator;
450458
$this->_catalogProduct = $catalogProduct;
@@ -481,6 +489,8 @@ public function __construct(
481489
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
482490
$this->orderIncrementIdChecker = $orderIncrementIdChecker ?: ObjectManager::getInstance()
483491
->get(\Magento\Sales\Model\OrderIncrementIdChecker::class);
492+
$this->allowedCountriesReader = $allowedCountriesReader
493+
?: ObjectManager::getInstance()->get(AllowedCountries::class);
484494
parent::__construct(
485495
$context,
486496
$registry,
@@ -941,7 +951,9 @@ public function assignCustomerWithAddressChange(
941951
/** @var \Magento\Quote\Model\Quote\Address $billingAddress */
942952
$billingAddress = $this->_quoteAddressFactory->create();
943953
$billingAddress->importCustomerAddressData($defaultBillingAddress);
944-
$this->setBillingAddress($billingAddress);
954+
if($this->isAddressAllowedForWebsite($billingAddress, (int)$this->getStoreId())) {
955+
$this->setBilliphpngAddress($billingAddress);
956+
}
945957
}
946958
}
947959

@@ -959,7 +971,9 @@ public function assignCustomerWithAddressChange(
959971
$shippingAddress = $this->_quoteAddressFactory->create();
960972
}
961973
}
962-
$this->setShippingAddress($shippingAddress);
974+
if ($this->isAddressAllowedForWebsite($shippingAddress, (int)$this->getStoreId())) {
975+
$this->setShippingAddress($shippingAddress);
976+
}
963977
}
964978

965979
return $this;
@@ -2596,4 +2610,18 @@ public function setExtensionAttributes(\Magento\Quote\Api\Data\CartExtensionInte
25962610
{
25972611
return $this->_setExtensionAttributes($extensionAttributes);
25982612
}
2613+
2614+
/**
2615+
* Check is address allowed for store
2616+
*
2617+
* @param Address $address
2618+
* @param int $storeId
2619+
* @return bool
2620+
*/
2621+
private function isAddressAllowedForWebsite(Address $address, int $storeId): bool
2622+
{
2623+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
2624+
2625+
return in_array($address->getCountryId(), $allowedCountries);
2626+
}
25992627
}

0 commit comments

Comments
 (0)