How to Add New State/Province and City Dropdowns in Magento 1.6.2
Adding new state/province and city dropdown options in Magento can enhance the checkout experience for your customers. In this guide, we’ll walk you through the process of extending this functionality for any country.
Understanding the Current Implementation
By default, Magento uses a file named app/code/core/Mage/Directory/Model/Resource/Region/Collection.php to fetch state/province data based on the selected country. This file is part of the core codebase and should not be modified directly.
Adding New States/Provinces for a Specific Country
To add new states or provinces for a specific country, you’ll need to use an SQL query. Here’s how to do it:
-
Log in to your database using PHPMyAdmin or any other preferred MySQL client.
-
Access the
core_dir_regiontable. -
Run the following SQL query to insert new state/province data for Ireland (replace the values with your own):
INSERT INTO core_dir_region (country_id, code, default_name)
VALUES ('IE', 'IE-D', 'Dublin');
-
Repeat step 3 for each additional state or province you want to add.
-
Save your changes and exit the database client.
Adding Cities Based on Selected State/Province
To create a city dropdown based on the selected state/province, we’ll need to modify some core Magento files. This involves creating an observer to intercept the core_block_abstract_prepare_layout_events event.
-
Create a new file in
app/code/local/[Your_Namespace]/[Your_Module]/etc/config.xml. -
Add the following XML configuration:
<config>
<events>
<core_block_abstract_prepare_layout_events>
<observers>
<your_module_add_cities_to_state_province>
<type>singleton</type>
<class>[Your_Namespace]_[Your_Module]/Observer</class>
<method>addCitiesToStateProvince</method>
</your_module_add_cities_to_state_province>
</observers>
</core_block_abstract_prepare_layout_events>
</events>
</config>
-
Create a new file in
app/code/local/[Your_Namespace]/[Your_Module]/Observer.php. -
Add the following code to the Observer class:
class [Your_Namespace]_[Your_Module]_Observer
{
public function addCitiesToStateProvince($observer)
{
$block = $observer->getBlock();
if ($block instanceof Mage_Directory_Block_Region) {
// Fetch cities based on selected state/province
$cities = Mage::getModel('[Your_Namespace]_[Your_Module]/City')->getCollection()->addFieldToFilter('region_id', $block->getSelectedRegionId());
// Add a new form field for the city dropdown
$field = array(
'name' => 'city',
'label' => Mage::helper('[Your_Namespace]_[Your_Module]')->__('City'),
'required' => true,
'options' => $cities->toOptionArray(),
);
// Add the city field to the form
$block->addField($field);
}
}
}
-
Create a new model file
app/code/local/[Your_Namespace]/[Your_Module]/Model/City.php. -
Implement your own logic in the City model to fetch cities based on the selected state/province.
-
Clear your Magento cache and run the deploy command (
php bin/magento setup:upgrade).
Conclusion
Adding new state/province and city dropdowns in Magento 1.6.2 requires a combination of SQL queries and custom observer methods. By following this guide, you should be able to extend the functionality for any country without modifying core files.
Remember that working with the Magento core can lead to conflicts when updating or upgrading your platform. To avoid potential issues, consider using extensions like the State/Province and Cities Manager by MageSolution or Amasty’s Address Book module, which provide built-in support for custom states, provinces, and cities.
As a Magento developer at LaravelCompany, I recommend exploring these extensions to enhance your store’s checkout process while ensuring compatibility with future updates.