
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.
Tags In
Related Posts
Leave a Reply Cancel reply
Categories
- Digital Marketing (7)
- eCommerce (34)
- Laravel (1)
- Latest News (5)
- Magento 2 Extensions (5)
- Magento2 (38)
- Shopify (5)
- Shopware (7)
- Wordpress (2)
Recent Posts
Advanced custom links extension (1) auto cancel order (1) banners (1) cancel order (1) cart attributes (1) Customer (7) Custom order invoice number module (1) custom shipping method (1) E-Commerce (7) eCommerce (41) full page cache magento 2 (1) hcaptcha (1) hcaptcha vs recaptcha (1) How to add customer attribute in Magento 2 (1) Laravel 10 (1) Magento2 (34) magento 2 back in stock notification (1) magento 2 cancel order (1) Magento2 Extension (3) magento 2 extensions (1) magento 2 layered navigation (1) magento 2 recaptcha (2) magento 2 robots.txt (1) magento 2.4.6 (1) magento headless commerce (1) new features (1) online store (13) openmaze (1) order management (1) Product (4) product attributes (1) recaptcha (1) search engine optimization (6) seo (10) shipping rule (1) shopify (3) shopware (6) Shopware-plugin (3) Shopware 6 (5) shopware 6 released (1) store switcher (1) structured data (1) upcoming update (1) wordpress (1) wordpress vs shopify (1)