How to add a new country in Magento 2 / Adobe Commerce

This article explains how to add a country that is not present in the Magento database.

Here I am using the example country code as “PR” and also I am hoping you already created a basic module.

Let’s follow step-by-step instructions to add a new country in Magento 2

Step 1: First, we need to create an “AddDataForAbstractCountry.php” file inside our extension at the below path.

app/code/Learn/ExtraCountries/Setup/Patch/Data/AddDataForAbstractCountry.php
<?php

declare(strict_types=1);

namespace Learn\ExtraCountries\Setup\Patch\Data;

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

/**
 * Add Abstract Country data to the country list
 *
 * @package Magento\ExtraCountries\Setup\Patch
 */
class AddDataForAbstractCountry 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
         * Add your custom country information
         */
        $data = [
            ['PR', 'PR', 'PRI']
        ];

        $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 '0.0.1';
    }

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

Step 2: After that, we need to create a “di.xml” file inside our extension at the below path.

app/code/Learn/ExtraCountries/etc/di.xml

Now, add the below code

<?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="Magento_Directory" type="Learn\ExtraCountries\Plugin\Framework\Locale\TranslatedListsPlugin"/>
    </type>
</config>

Step 3: After that, we need to create a “TranslatedListsPlugin.php” file inside our extension at the below path.

app/code/Learn/ExtraCountries/Plugin/Framework/Locale/TranslatedListsPlugin.php

Now, add the below code

<?php

declare(strict_types=1);

namespace Learn\ExtraCountries\Plugin\Framework\Locale;

use Magento\Framework\Locale\ListsInterface;

/**
 * Plugin to add full names of added countries that are not included in Zend Locale Data
 */
class TranslatedListsPlugin
{
    /**
     * Get the full name of added countries
     *
     * Since the locale data for the added country may not be present in the Zend Locale Library,
     * we need to provide full country name matching the added code
     *
     * @param ListsInterface $subject
     * @param callable $proceed
     * @param $value
     * @param null $locale
     * @return string
     */
    public function aroundGetCountryTranslation(
        ListsInterface $subject,
        callable       $proceed,
                       $value,
                       $locale = null
    )
    {
        //new country code
        if ($value == 'PR') {
            return 'Puerto Rico';
        }
        return $proceed($value, $locale);
    }
}

Step 4: After that, we need to create a “config.xml” file inside our extension at the below path.

app/code/Learn/ExtraCountries/etc/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <general>
            <country>
                <!-- append a new country codes to the end of this list -->
                <allow>AF,AL,DZ,AS,AD,AO,AI,AQ,AG,AR,AM,AW,AU,AT,AX,AZ,BS,BH,BD,BB,BY,BE,BZ,BJ,BM,BL,BT,BO,BQ,BA,BW,BV,BR,IO,VG,BN,BG,BF,BI,KH,CM,CA,CD,CV,KY,CF,TD,CL,CN,CX,CW,CC,CO,KM,CG,CK,CR,HR,CU,CY,CZ,DK,DJ,DM,DO,EC,EG,SV,GQ,ER,EE,ET,FK,FO,FJ,FI,FR,GF,PF,TF,GA,GM,GE,DE,GG,GH,GI,GR,GL,GD,GP,GU,GT,GN,GW,GY,HT,HM,HN,HK,HU,IS,IM,IN,ID,IR,IQ,IE,IL,IT,CI,JE,JM,JP,JO,KZ,KE,KI,KW,KG,LA,LV,LB,LS,LR,LY,LI,LT,LU,ME,MF,MO,MK,MG,MW,MY,MV,ML,MT,MH,MQ,MR,MU,YT,FX,MX,FM,MD,MC,MN,MS,MA,MZ,MM,NA,NR,NP,NL,AN,NC,NZ,NI,NE,NG,NU,NF,KP,MP,NO,OM,PK,PW,PA,PG,PY,PE,PH,PN,PL,PS,PT,PR,QA,RE,RO,RS,RU,RW,SH,KN,LC,PM,VC,WS,SM,ST,SA,SN,SC,SL,SG,SK,SI,SB,SO,ZA,GS,KR,ES,LK,SD,SR,SJ,SZ,SE,CH,SX,SY,TL,TW,TJ,TZ,TH,TG,TK,TO,TT,TN,TR,TM,TC,TV,VI,UG,UA,AE,GB,US,UM,UY,UZ,VU,VA,VE,VN,WF,EH,XK,YE,ZM,ZW,PR</allow>
            </country>
        </general>
    </default>
</config>

Bonus step: GraphQL to add a custom country in the list for this we need to create a “schema.graphqls” file inside our extension at the below path.

app/code/Learn/ExtraCountries/etc/schema.graphqls
enum CountryCodeEnum @doc(description: "The list of countries codes") {
    PR @doc(description: "Puerto Rico")
}

Thank you so much for reading this blog keep supporting me and I will come up with a new blog.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *