How to add a new country in Magento 2?

In this section, we will explore how can we add a new country to Magento2. However, there are all countries listed in Magneto 2 however, there are a few islands that are not listed. It’s important to have them on the country list so the product can be delivered worldwide.

First of all, create a module, let’s start with the registration.php file. Create app/code/WebbyTroops/NewCountry/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'WebbyTroops_NewCountry',
    __DIR__
);

Create a file at app/code/WebbyTroops/NewCountry/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="WebbyTroops_NewCountry" >
        <sequence>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

Add the new country code in database, for that we need to create a file in app/code/WebbyTroops/NewCountry/Setup/Patch/Data/NewIreland.php

<?php

namespace WebbyTroops\NewCountry\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;

/**
 * Class AddDataForNorthernIreland
 */
class NewIreland 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 = [
            ['BQ', 'BQ', 'BES'] //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.3.3';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases() {
        return [];
    }

}

Create a new file di.xml at app/code/WebbyTroops/NewCountry/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="webbyTroops_newcountry" type="WebbyTroops\NewCountry\Plugin\Framework\Locale\TranslatedListsPlugin"/>
    </type>
</config>

Create file for plugin at app/code/WebbyTroops/NewCountry/Plugin/Framework/Locale/TranslatedListsPlugin.php

<?php

namespace WebbyTroops\NewCountry\Plugin\Framework\Locale;

use Magento\Framework\Locale\ListsInterface;

/**
 * @inheritdoc
 */
class TranslatedListsPlugin
{
    /**
     * @inheritdoc
     */
    public function aroundGetCountryTranslation(
        ListsInterface $subject,
        callable $proceed,
        $value,
        $locale = null
    ) {
        if ($value == 'BQ') {
            /* need to add $locale selector */
            return 'Bonaire, Sint Eustatius and Saba';
        }
        return $proceed($value, $locale);
    }
}

At the last the general deploy commands that require to run in the following sequence:

php bin/magento setup:upgrade;
php bin/magento setup:di:compile;
php bin/magento cache:flush;

That’s all. In backend Store > Configuration > General > General > Country Options you require to select this new country in the Allowed Country options.

Leave A Comment

Go To Top