Search This Blog

Jan 30, 2011

Removing certain States in Magento

Magento supports multi store, multi language, multi site and other multi stuff. As its support multi language it contains complete collection of both country and state. Several reasons why store owners’ want to remove state/region in the checkout and registration section are the following:

1. The scope of shipping location that their store caters.
2. The delivery capacity of the company.
3. Shipping method is not allowed with that state.
4. Or simply just get rid off in the list.



Alternative Solution


Most developer/even owner with an extra “tech skill” often do a manual deletion in magento database. They remove specific state in the table to narrow down the list. It is an easy-to-do way but has a pitfall in terms of upgrading magento. When you update magento those state deleted will be back. 

Best Solution:

Removing it in different approach :
1. copy the collection.php located in /app/code/core/Mage/Directory/Model/Mysql4/Region/Collection.php
2. create directory and paste it.
/app/code/local/ Mage/Directory/Model/Mysql4/Region/Collection.php

Find this code:

$this->_select->from(array('region'=>$this->_regionTable),
    array('region_id'=>'region_id', 'country_id'=>'country_id', 'code'=>'code', 'default_name'=>'default_name')
);

Replace it with:
$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);

$exclude_regions = array ('AS','AK','AA','AC','AE','AK','AM','AP','FM','GU','HI','MH','MP','PW','PR','VI','AF'); Please take note that 'AS', AK' are state abbreviation that will be excluded. You can view these abbreviation either in search engine or in database.

14 comments:

Anonymous said...

Is there any way that I can make it with country collection?

Janzell said...

Hi anonymous, I also looking for an easy task to do that.How about simply do the same coding for the country collection.

If doesn't work you can manually delete it in database.

Qtr52 said...

You can exclude countries from the admin panel

Qtr52 said...

You can exclude countries from the admin panel

Janzell Jurilla said...

HI Qtr52,

Thanks for dropping by. I'm wondering where is the configuration of the country?

Adam Malkin said...

Thanks, this worked Great, i also added a sort onto the collection whilst i was in there by adding the following uner your code:

$this->setOrder('code', self::SORT_ORDER_ASC);

Janzell Jurilla said...

Hi Adam, thanks for the sweet words and thank  you for sharing the sorting code. 

Eltrino said...

Hi everyone - we created and extension to handle this.
http://www.magentocommerce.com/magento-connect/catalog/product/view/id/12349/

Anonymous said...

If you decide you do NOT want to ship to certain states within the united states, you could do a simple database hack and remove any state you wish, here's how (note: i am using Magento Version 1.6.2):

Get into your database via phpMyAdmin or SSH, open the table 'directory_country_origin' and simply delete the rows (states) you don't want showing up in your frontend.

For example I do not ship to puerto rico so i removed line 52 from the 'directory_country_origin' table. Now it no longer shows in the dropdown at checkout.

Quick n dirty workaround but nevertheless...

Janz said...

Hi Anon,

Thanks for the tip... It is really helpful even though it is a hack.

-Janzell

Anonymous said...

This information was moved in version 1.6, but here is a hard sought after new version fix I just found. In the text ArtP says that it is for v1.5 but it is actually for v1.6 as he added later. Works like a charm!

http://magentoexpert.com/questions/how-do-i-remove-certain-states-from-stateregion-list-at-checkout-or-registration-in-magento-1-6/

Anonymous said...

Here is another extension that can be used for that:
http://www.magentocommerce.com/magento-connect/zones-manager-4902.html

Unknown said...

In my enterprise edition 1.12.0, i am not able to find this particular code.

Unknown said...

Hi,
I am using magento enterprise edition 1.12.0, and i am not able to find this particular code in my magento directory.