You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having at least 2 websites (scopes) configured in Magento
Each website has its own list of available countries. (E.g. site1 has only the Netherlands and Belgium, while site2 has only Germany).
One of the websites domain is being used for accessing and using the admin area (e.g. site1).
Click on 'POSTNL' menu item in the admin navigation on the left.
Click on 'Matrix Rates'.
Add a new matrixrate (country: Germany, other values don't matter) for site2 (available countries in the configuration for site2 = [Germany]).
Visit 'Matrix Rates' again to overview the grid with the rates.
Expected result
We see the list with the matrix rates created in our Magento installation.
Actual result
We see the message 'The country isn't available', which is an exception thrown when calling (in Ui/Component/Matrix/Listing/Columns/Country.php):
Workaround
Enable the country for site1, because in getCountryInfo the website configuration of current admin store view (so website site1 and not site2) is used.
Or (rewrite the country name retrieval a bit differently). My own class example (<column name="website_id" class="BytesGuru\PostNL\Ui\Component\Matrix\Listing\Columns\Website"/>):
<?phpdeclare(strict_types=1);
namespaceBytesGuru\PostNL\Ui\Component\Matrix\Listing\Columns;
useMagento\Backend\Model\Auth\SessionasAdminSession;
useMagento\Directory\Model\AllowedCountries;
useMagento\Directory\Model\CountryasDirectoryCountry;
useMagento\Directory\Model\ResourceModel\Country\CollectionasCountryCollection;
useMagento\Directory\Model\ResourceModel\Country\CollectionFactoryasCountryCollectionFactory;
useMagento\Framework\Exception\NoSuchEntityException;
useMagento\Framework\View\Element\UiComponent\ContextInterface;
useMagento\Framework\View\Element\UiComponentFactory;
useMagento\Store\Model\ScopeInterface;
useMagento\Ui\Component\Listing\Columns\Column;
classCountryextendsColumn
{
privateCountryCollectionFactory$countryCollectionFactory;
/** * @var array<int, CountryCollection<DirectoryCountry>> */privatearray$countryCollectionPerWebsite;
privateAllowedCountries$allowedCountries;
privateAdminSession$adminSession;
/** * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param CountryCollectionFactory $countryCollectionFactory * @param AllowedCountries $allowedCountries * @param AdminSession $adminSession * @param array<string, mixed> $components * @param array<string, mixed> $data */publicfunction__construct(
ContextInterface$context,
UiComponentFactory$uiComponentFactory,
CountryCollectionFactory$countryCollectionFactory,
AllowedCountries$allowedCountries,
AdminSession$adminSession,
array$components = [],
array$data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->countryCollectionFactory = $countryCollectionFactory;
$this->allowedCountries = $allowedCountries;
$this->adminSession = $adminSession;
}
/** * Prepare Data Source * * @see Website::prepareDataSource() * * @param array<string, mixed> $dataSource * * @return array<string, mixed> */publicfunctionprepareDataSource(array$dataSource): array
{
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as &$item) {
$itemList = explode(',', $item[$fieldName]);
$countryNameList = [];
foreach ($itemListas$value) {
try {
// _website_id is set in \BytesGuru\PostNL\Ui\Component\Matrix\Listing\Columns\Website$countryNameList[] = $this->getFullCountryName($value, $item['_website_id']);
} catch (NoSuchEntityException$e) {
$countryNameList[] = __(
"Country by code '%1' is not enabled for the website '%2'.",
$value,
$item['_website_id']
);
}
}
$item[$fieldName] = implode(', ', $countryNameList);
}
}
return$dataSource;
}
/** * Get the full name of a country by its ID. * * @param string $countryId The ID of the country. * @param int|string $websiteId The ID of the website. * * @return string The full name of the country (based on the current admin user locale). * * @throws NoSuchEntityException If the country is not available. */privatefunctiongetFullCountryName(string$countryId, $websiteId): string
{
$websiteId = (int) $websiteId;
if (isset($this->countryCollectionPerWebsite[$websiteId])) {
$countryCollection = $this->countryCollectionPerWebsite[$websiteId];
} else {
$this->countryCollectionPerWebsite[$websiteId] = $this->countryCollectionFactory->create();
$countryCollection = $this->countryCollectionPerWebsite[$websiteId];
$allowedCountries = $this->allowedCountries->getAllowedCountries(
ScopeInterface::SCOPE_WEBSITE,
(string) $websiteId
);
if ($allowedCountries) {
$countryCollection->addFieldToFilter('country_id', ['in' => $allowedCountries])->load();
}
}
/** @var \Magento\Directory\Model\Country|null $country */$country = $countryCollection->getItemById($countryId);
if (!$country) {
thrownewNoSuchEntityException(__("The country isn't available."));
}
$currentUser = $this->adminSession->getUser();
$currentAdminLocale = $currentUser ? $currentUser->getInterfaceLocale() : 'en_US';
return$country->getName($currentAdminLocale);
}
}
Note: we don't have the website_id available on the moment of \TIG\PostNL\Ui\Component\Matrix\Listing\Columns\Country::prepareDataSource execution, because it has been overwritten by its name for each row in \TIG\PostNL\Ui\Component\Matrix\Listing\Columns\Website::prepareDataSource. So the altering is required there as well or another method should be used for getting the full country name for each row.
Screenshots
Magento version: 2.4.3-p3
PHP version: 7.4.28
TIG PostNL version: 1.15.2
The text was updated successfully, but these errors were encountered:
To Reproduce
Steps to reproduce the behavior:
Expected result
We see the list with the matrix rates created in our Magento installation.
Actual result
We see the message 'The country isn't available', which is an exception thrown when calling (in Ui/Component/Matrix/Listing/Columns/Country.php):
Workaround
Enable the country for site1, because in
getCountryInfo
the website configuration of current admin store view (so website site1 and not site2) is used.Or (rewrite the country name retrieval a bit differently). My own class example (
<column name="website_id" class="BytesGuru\PostNL\Ui\Component\Matrix\Listing\Columns\Website"/>
):Note: we don't have the website_id available on the moment of
\TIG\PostNL\Ui\Component\Matrix\Listing\Columns\Country::prepareDataSource
execution, because it has been overwritten by its name for each row in\TIG\PostNL\Ui\Component\Matrix\Listing\Columns\Website::prepareDataSource
. So the altering is required there as well or another method should be used for getting the full country name for each row.Screenshots
![matrix-rates-exception](https://private-user-images.githubusercontent.com/15354052/356360584-699b315c-d0b0-4097-ab57-6811f2b01ba3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjYxMDUwNzksIm5iZiI6MTcyNjEwNDc3OSwicGF0aCI6Ii8xNTM1NDA1Mi8zNTYzNjA1ODQtNjk5YjMxNWMtZDBiMC00MDk3LWFiNTctNjgxMWYyYjAxYmEzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTEyVDAxMzI1OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTkwNjAzYzUxNDIwMDY4ODI3MjgxZDFmN2ZmYWVjZGVjY2EyMGFiZmExNDIxOTA0YTNhNWNiZTNjZmJhZjlmNTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.b9eMfegP0egsfspqBz7s5AepiU4w4lIimE0i2kPV7MA)
The text was updated successfully, but these errors were encountered: