How to add a new country in Magento 2?
22 April 2022

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 {
    private $moduleDataSetup;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
    }

    public function apply() {
        $data = [
            ['BQ', 'BQ', 'BES'] //iso3_code ???
        ];

        $columns = ['country_id', 'iso2_code', 'iso3_code'];
        $this->moduleDataSetup->getConnection()->insertArray(
            $this->moduleDataSetup->getTable('directory_country'),
            $columns,
            $data
        );
    }

    public static function getDependencies() {
        return [];
    }

    public static function getVersion() {
        return '2.3.3';
    }

    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;

class TranslatedListsPlugin
{
    public function aroundGetCountryTranslation(
        ListsInterface $subject,
        callable $proceed,
        $value,
        $locale = null
    ) {
        if ($value == 'BQ') {
            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.

Categories

Download The Free E-book & Launch Your Brand Strategically

Download The Free E-book & Launch Your Brand Strategically

Share this post