Skip to content

Allowed Countries does not work correctly #38182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions app/code/Magento/Customer/Model/Address/Validator/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\Validator\ValidateException;
use Magento\Framework\Validator\ValidatorChain;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Address country and region validator.
Expand All @@ -36,18 +37,26 @@ class Country implements ValidatorInterface
*/
private $allowedCountriesReader;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param Data $directoryData
* @param AllowedCountries $allowedCountriesReader
* @param StoreManagerInterface $storeManager
* @param Escaper|null $escaper
*/
public function __construct(
Data $directoryData,
AllowedCountries $allowedCountriesReader,
StoreManagerInterface $storeManager,
Escaper $escaper = null
) {
$this->directoryData = $directoryData;
$this->allowedCountriesReader = $allowedCountriesReader;
$this->storeManager = $storeManager;
$this->escaper = $escaper ?? ObjectManager::getInstance()->get(
Escaper::class
);
Expand Down Expand Up @@ -134,7 +143,13 @@ private function validateRegion(AbstractAddress $address)
*/
private function getWebsiteAllowedCountries(AbstractAddress $address): array
{
$storeId = $address->getData('store_id');
return $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
$scopeCode = $address->getData('store_id');
$scope = ScopeInterface::SCOPE_STORE;
if (!$scopeCode && $address->getCustomer() && $address->getCustomer()->getSharingConfig()?->isGlobalScope()) {
$scopeCode = array_keys($this->storeManager->getWebsites(false, true));
$scope = ScopeInterface::SCOPE_WEBSITES;
}

return $this->allowedCountriesReader->getAllowedCountries($scope, $scopeCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,10 @@ public function getAllOptions($withEmpty = true, $defaultValues = false)

$allowedCountries = array_unique(array_merge([], ...$allowedCountries));
} else {
// Address can be added only for the allowed country list.
$websiteId = null;
$customerId = $this->request->getParam('parent_id') ?? null;
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$websiteId = $customer->getWebsiteId();
}

$websiteCodes = array_keys($this->storeManager->getWebsites(false, true));
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(
ScopeInterface::SCOPE_WEBSITE,
$websiteId
ScopeInterface::SCOPE_WEBSITES,
$websiteCodes
);
}

Expand Down