Table of Contents

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

Create a file at app/code/WebbyTroops/NewCountry/etc/module.xml



    
        
            
        
    

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

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



    
        
    

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

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.

Share this article f t in