6

The list of regions for USA contains: Alaska, Hawaii, American Samoa, Guam, Marshall Islands, Micronesia and Armed American and so on. This is the list that is available both during registration and on checkout.

So If someone want to remove the state/region named Alaska, Hawaii, American Samoa, Guam, Marshall Islands. How we can achieve this?

4 Answers 4

10

This can be easily achievable without removing from "directory_country_region" database table.

Just you need to follow below steps:

  1. Override _getRegions($storeId) method of Mage_Directory_Helper_Data class.
  2. Assign the Region Codes which you want to exclude into an array i.e $excludeRegions variable.
  3. 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,

1
  • 1
    It might be helpful to point out that this file should be located at app/code/local/Mage/Directory/Helper/Data.php
    – iJeep
    Commented Jul 6, 2016 at 14:55
2

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!

0

There is no native method to remove those.

You have to remove those region from the database only.

Table Name : directory_country_region

Note: This method is not recommended

0

If you want to hide in checkout/onepage i use this simpls js code:

path:app/design/frontend/rwd/default/template/checkout/onepage/shipping.phtml

var siRegions = <?php echo $this->helper('directory')->getRegionJson() ?>;
var excludeRegions =['AL', 'AK'];//you regions here
  var keys = Object.keys(siRegions.US);
  for (var i = keys.length-1; i >= 0; i--){
    if (excludeRegions.indexOf(siRegions.US[keys[i]].code) != -1){
      delete siRegions.US[keys[i]];
    }
  }

}

deleteRegion();

Important: Find <?php echo $this->helper('directory')->getRegionJson() ?>; and swap with siRegions. It is in the same file always. Thats it. Hope this help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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