By default Magento consists of all the countries and doesn’t have the states for all these countries. Unfortunately we didnt have these options in admin to do add states or region. So to add states or region to a country in magento we have to do some tweaks to the database,ie adding the states to the database

We have two tables in database which stores the region details

  1. directory_country_region
  2. directory_country_region_name

When we add record to directory_country_region it will enable the selectbox for the country we have added and we have the options to make the region required too.
Then we have to add the desired regions to the second table directory_country_region_name

Here is the code to add regions for India

We can run this code outside magento installation by using the code given below

<?php
require_once 'app/Mage.php';
Mage::init();
//Add the code to excecute
?>
// set up array containing regions to be added (region_code => region_name)
$new_regions = array(
	'Kerala' => "Kerala",
	'Karnataka' => "Karnataka",
);
 
// specify country code for new regions
$country_code = 'IN';
 
// specify locale
$locale = 'en_US';
 
// create our core_write conection object
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
 
// iterate our new regions
foreach ($new_regions as $region_code => $region_name) {
 
	// insert region 
	$sql = "INSERT INTO `directory_country_region` (`region_id`,`country_id`,`code`,`default_name`) VALUES (NULL,?,?,?)";
	$connection->query($sql,array($country_code,$region_code,$region_name));
 
	// get new region id for next query
	$region_id = $connection->lastInsertId();
 
	// insert region name
	$sql = "INSERT INTO `directory_country_region_name` (`locale`,`region_id`,`name`) VALUES (?,?,?)";
	$connection->query($sql,array($locale,$region_id,$region_name));
}

This code will add the regions we provided in the array to the magento ie we can add states or region to a country in magento with lot of ease

If you are not interested in writing codes there are extensions availbale in magento connect in which one of them is free
Regions Manager

Leave a Reply

Your email address will not be published.