Magento – Limit shipping per states on checkout

checkoutmagento-1.9shipping

I need to limit the shipping to some specific states on Magento.

Magento version is CE 1.9.2 most recent.

I found an article for this issue ( http://doejo.com/magento-how-to-remove-certain-states-from-state-region-list-at-checkout-or-registration/ )

According to article, I need to modify the /app/code/core/Mage/Directory/Model/Mysql4/Region/Collection.php file in a following way.

At around line 50, change the code below:

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

To following:

$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);

Now, I tried to duplicate the same file at the local pool first, since editing the magento core files is never a good idea… However, That file has only 3 lines of code:

class Mage_Directory_Model_Mysql4_Region_Collection extends Mage_Directory_Model_Resource_Region_Collection
{
}

Any ideas where the code is moved? Which file do I need to modify on recent magento version? :))

Thank you

Best Answer

The article was written in 2010 (with a maximum of Magento 1.5), and things have changed a little since then. Particularly, the file that this article said to copy/edit is now deprecated. The new file has much code that is different.

You need to copy app/code/core/Mage/Directory/Model/Resource/Region/Collection.php. When you open it up, jump down to line 75 and add:

$excludeRegions = array ('AS','AK','AA','AC','AE','AK','AM','AP','FM','GU','HI','MH','MP','PW','PR','VI','AF');

$this->getSelect()->where('code NOT IN (?)', $excludeRegions);

This will do the trick. I would suggest that you do not copy files, and instead write a module override/rewrite this class, so you can just use this function:

protected function _initSelect()
{
    parent::_initSelect();
    $excludeRegions = array ('AS','AK','AA','AC','AE','AK','AM','AP','FM','GU','HI','MH','MP','PW','PR','VI','AF');
    $this->getSelect()->where('code NOT IN (?)', $excludeRegions);

    return $this;
}

With this, Magento can change their core code on the page, and you are just doing one little tiny thing and not affecting anything else.

Related Topic