Add new nation into nation's list in Magento 2.3.5
-
15-04-2021 - |
Question
I want to add a new country Northern Ireland
with the country code XI
in Magento 2.3.5 Admin. any idea for this?
I already tried with this -
Add new country in default country dropdown in Magento 2.3.5
I found the same answer everywhere but it does not work for me so please help me with this.
if I want to update the data in "ICUDATA-region" so can it be possible?
La solution
Create a module
module registration file
Vendor/NorthernIreland/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_NorthernIreland',
__DIR__
);
module initialization file
Vendor/NorthernIreland/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_NorthernIreland" >
<sequence>
<module name="Magento_Directory"/>
</sequence>
</module>
</config>
file di, register plugin
Vendor/NorthernIreland/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Locale\TranslatedLists">
<plugin name="vendor_northernireland" type="Vendor\NorthernIreland\Plugin\Framework\Locale\TranslatedListsPlugin"/>
</type>
</config>
plugin
Vendor/NorthernIreland/Plugin/Framework/Locale/TranslatedListsPlugin.php
<?php
namespace Vendor\NorthernIreland\Plugin\Framework\Locale;
use Magento\Framework\Locale\ListsInterface;
/**
* @inheritdoc
*/
class TranslatedListsPlugin
{
/**
* @inheritdoc
*/
public function aroundGetCountryTranslation(
ListsInterface $subject,
callable $proceed,
$value,
$locale = null
) {
if ($value == 'XI') {
/* need to add $locale selector */
return 'Northern Ireland';
}
return $proceed($value, $locale);
}
}
db path, add code XI to directory_country table
Vendor/NorthernIreland/Setup/Patch/Data/AddDataForNorthernIreland.php
<?php
namespace Vendor\NorthernIreland\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
/**
* Class AddDataForNorthernIreland
*/
class AddDataForNorthernIreland implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup
) {
$this->moduleDataSetup = $moduleDataSetup;
}
/**
* {@inheritdoc}
*/
public function apply()
{
/**
* Fill table directory/country
*/
$data = [
['XI', 'XI', 'XI'] //iso3_code ???
];
$columns = ['country_id', 'iso2_code', 'iso3_code'];
$this->moduleDataSetup->getConnection()->insertArray(
$this->moduleDataSetup->getTable('directory_country'),
$columns,
$data
);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getVersion()
{
return '2.0.0';
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
then run console commands
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy (optional)
check your system configuration (Stores/Setting/Configuration/General/General/Country Options), Northern Ireland should appear on all listings in this section.
I hope this helps you.