Magento: How to remove certain States from state/region list at Checkout or registration?
This can be easily achievable without removing from "directory_country_region" database table.
Just you need to follow below steps:
- Override _getRegions($storeId) method of Mage_Directory_Helper_Data class.
- Assign the Region Codes which you want to exclude into an array i.e $excludeRegions variable.
- Add logic for skip above region codes from available list
So the final code looks like as below:
$excludeRegions = array('AK','AS','AF','AA','AC','AE','AM','AP','DC','FM','GU','HI','MH','MP','PW','PR','VI');
foreach ($collection as $region) {
if (!$region->getRegionId()) {
continue;
}
//BOF Custom Logic Here
$regionCode = $region->getCode();
if (in_array($regionCode, $excludeRegions)) {
continue;
}
//EOF Custom Logic Here
$regions[$region->getCountryId()][$region->getRegionId()] = array(
'code' => $region->getCode(),
'name' => $this->__($region->getName())
);
}
Hoping this will help to someone.
Thanks,
Magento 1.9.2
Step 1: Copy file from: app/code/core/Mage/Directory/Helper/Data.php
to: app/code/local/Mage/Directory/Helper/Data.php
Step 2: Override _getRegions($storeId) method of Mage_Directory_Helper_Data class.
- Look at the line number 184: protect function _getRegions($storeID)
- Add this code after the end of the $regions on line 200
$excludeRegions = array('AS','AF','AA','AC','AE','AM','AP','DC','FM','GU','MH','MP','PW','PR','VI'); foreach ($collection as $region) { if (!$region->getRegionId()) { continue; }
Step 3: Assign the Region Codes which you want to remove to an array i.e. $excludeRegions variable.
- Add this codes after step 2.
//BOF Custom Logic Here $regionCode = $region->getCode(); if (in_array($regionCode, $excludeRegions)) { continue; }
Step 4: Assign the Region Codes which you want to remove to an array i.e. $excludeRegions variable.
Below is the final code:
==========================================================================
protected function _getRegions($storeId)
{
$countryIds = array();
$countryCollection = $this->getCountryCollection()->loadByStore($storeId);
foreach ($countryCollection as $country) {
$countryIds[] = $country->getCountryId();
}
/** @var $regionModel Mage_Directory_Model_Region */
$regionModel = $this->_factory->getModel('directory/region');
/** @var $collection Mage_Directory_Model_Resource_Region_Collection */
$collection = $regionModel->getResourceCollection()
->addCountryFilter($countryIds)
->load();
$regions = array(
'config' => array(
'show_all_regions' => $this->getShowNonRequiredState(),
'regions_required' => $this->getCountriesWithStatesRequired()
)
);
$excludeRegions = array('AS','AF','AA','AC','AE','AM','AP','DC','FM','GU','MH','MP','PW','PR','VI');
foreach ($collection as $region) {
if (!$region->getRegionId()) {
continue;
}
//BOF Custom Logic Here
$regionCode = $region->getCode();
if (in_array($regionCode, $excludeRegions)) {
continue;
}
//EOF Custom Logic here
$regions[$region->getCountryId()][$region->getRegionId()] = array(
'code' => $region->getCode(),
'name' => $this->__($region->getName())
);
}
return $regions;
}
Step 5: Log in to your Magento admin and clear all cache.
Hope this help!