2

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?

4

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.

4
  • Thanks for the reply. I will check
    – Kushal
    Feb 11 at 7:47
  • I just want to know that do you know "icuDATA-region" and if I want to update the data in that so can it be possible?
    – Kushal
    Feb 11 at 7:52
  • sorry, now I don't know how you can change this
    – Vitalii
    Feb 11 at 8:25
  • thanks, your solution solves my problem.
    – Kushal
    Feb 11 at 8:26
0

@vitalii hi

$data = [['XI', 'XI', 'XII'],['ZB', 'ZB', 'ZBB'] ];

to add multiple countries we have to do something like this ??

I have created the same extension with the help of Magento link https://support.magento.com/hc/en-us/articles/360057988152-How-to-add-a-new-country-to-Magento-2-

but I have already added countries code to the table instead of creating patch

but in may case plugin giving error on return

    return $proceed($value, $locale);

this return syntax error as $value have not value why ? please help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.