0

I want to add some specific states options in State drop down on checkout page for UAE country option.

I didn't find any setting for this.

I have to create a custom module or need to change in any file? What should I do?

  • In Magento2 any setting not available for add new states for the particular country. If you are need then run sql query in database. – Abdul Apr 6 '18 at 5:05
  • @Abdul, Can you please post an example for that query? – Vinaya Maheshwari Apr 6 '18 at 5:11
  • Try this: INSERT INTO directory_country_region VALUES (NULL , 'AE', 'AUH','Abu Dhabir'), (NULL , 'AE', 'AJM','Ajman'), (NULL , 'AE', 'SHJ','Sharjah'), (NULL , 'AE', 'DXB','Dubai'), (NULL , 'AE', 'FUJ','Fujairah'), (NULL , 'AE', 'RAK','Ras Al Khaimah'), (NULL , 'AE', 'UAQ','Umm Al Quwain'); – Abdul Apr 6 '18 at 5:38
  • Thanks it is working, But Is there any way to add specific to one website as I have multi websites, right now options are showing for all websites. – Vinaya Maheshwari Apr 6 '18 at 5:44
  • have you need UAE only one website? – Abdul Apr 6 '18 at 6:01
1

Here is the way you can insert new states to your Magento 2 via the setup file just follow the below steps

  • Create a new module at Magento_root/app/code/{Vendor}/{Module}
  • create below files into it as per the locations

Magento_root/app/code/{Vendor}/{Module}/etc/module.xml Magento_root/app/code/{Vendor}/{Module}/registration.php Magento_root/app/code/{Vendor}/{Module}/Setup/InstallData.php Magento_root/app/code/{Vendor}/{Module}/etc/book1.csv

Code for InstallData.php

<?php

namespace Abhishek\Addstate\Setup;

use \Magento\Framework\Setup\InstallDataInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\DB\Ddl\Table;

class InstallData implements InstallDataInterface{

    public function __construct(
        \Magento\Framework\Module\Dir\Reader $moduleReader,
        \Magento\Framework\File\Csv $fileCsv 
    ){
        $this->_moduleReader =$moduleReader;
        $this->_fileCsv =$fileCsv;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){

        $setup->startSetup();
        $data = $this->getCSV();

        $num = count($data);
        echo "<pre>";
            for ($c=1; $c < $num; $c++) {
                $data1 = $data[$c];  
                $code = $data1[0];
                $StateName = $data1[1];
                $setup->getConnection()->Insert(
                    $setup->getTable('directory_country_region'),
                    [
                        'country_id' => 'IN',
                        'code' => $code,
                        'default_name' => $StateName
                    ]
                );
            }

        $setup->endSetup();
    }

    public function getCSV(){
        $directory = $this->_moduleReader->getModuleDir('etc', 'Abhishek_AddState');
        $file = $directory . '/book1.csv';
        if (file_exists($file)) {
        $data = $this->_fileCsv->getData($file);
        return $data;
        }
    }
}

book1.csv

code,Statename
AN,Andaman Nicobar
AP,Andhra Pradesh
AR,Arunachal Pradesh
AS,Assam
BH,Bihar
CH,Chandigarh
CG,Chhattisgarh
DN,Dadra Nagar Haveli
DD,Daman Diu
DL,Delhi
GA,Goa
GJ,Gujarat
HR,Haryana
HP,Himachal Pradesh
JK,Jammu Kashmir
JH,Jharkhand
KA,Karnataka
KL,Kerala
LD,Lakshadweep
MP,Madhya Pradesh
MH,Maharashtra
MN,Manipur
ML,Meghalaya
MZ,Mizoram
NL,Nagaland
OR,Odisha
PY,Pondicherry
PB,Punjab
RJ,Rajasthan
SK,Sikkim
TN,Tamil Nadu
TG,Telangana
TR,Tripura
UP,Uttar Pradesh
UT,Uttaranchal
WB,West Bengal
  • Now run the below command from your Magento_root in terminal

    php bin/magento setup:upgrade

Here in my answer I have used Indian States for example you can use whatever you want but make sure to use the country_id as per available into the database

Note: do not run the sql queries directly or do not do entries directly to > the data base it is not considered as a good practice of Magento as preferred > that to insert any data into the database use setup script always

  • I agree with you, but please check my last comment above, I have multi-website and only want to add state options for one website only. – Vinaya Maheshwari Apr 6 '18 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.