Magento: How to remove certain States from state/region list at Checkout or registration.

Magento is an e-commerce platform that supports multi-language and all that multi stuff. Therefore list of Regions per country is perfectly legal. The list of regions for USA contains: Alaska, Hawaii, American Samoa, Guam, Marshall Islands, Micronesia and Armed American Forces all over the world. This is the list that is available both during registration and on checkout. There are a lot of store owners/developers/designers who want to remove some of the items from that list. Why?

Plenty of reasons:

  • e-commerce store offers free shipping and doesn’t want to ship to Guam for free.
  • risk of items not being delivered, problems in shipping.
  • Simply to shorten that drop-down region list.
  • or… simply store owner just doesn’t want to ship to.. say, Alabama.

How most of people approach this task. Well.. most people, in order to avoid those regions being listed, go to phpMyAdmin magento database and manually delete all of those region id’s and such. I don’t think this is the best way to go, as upon upgrade, you’ll get those states back in the region list.

Here’s a different approach of removing ‘unwanted’ states from magento region list. Simply override Region collection, so that those codes are being filtered out. Here’s what to do:

  • copy file at <magento installation>/app/code/core/Mage/Directory/Model/Mysql4/Region/Collection.php to your local code directory at <magento installation>/app/code/local/Mage/Directory/Model/Mysql4/Region/Collection.php
  • change this line of code (around line 50):
$this->_select->from(array('region'=>$this->_regionTable),
    array('region_id'=>'region_id', 'country_id'=>'country_id', 'code'=>'code', 'default_name'=>'default_name')
);
  • to this:
$exclude_regions = array ('AS','AK','AA','AC','AE','AK','AM','AP','FM','GU','HI','MH','MP','PW','PR','VI','AF');

$this->_select->from(array('region'=>$this->_regionTable),
    array('region_id'=>'region_id', 'country_id'=>'country_id', 'code'=>'code', 'default_name'=>'default_name')
)
->where('code NOT IN (?)', $exclude_regions);

This filters out a lot of unnecessary states from query and doesn’t show them on front-end of magento. In addition, you can remove any other states you want, just look up codes in the magento DB.

Filed in: Web Development