1

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

1
  • Any ideas please? :))
    – Domeniko
    Oct 3, 2015 at 19:38

2 Answers 2

3

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.

2
  • Hi @joseph Maxwell. Thanks a lot for response. Im gonna give this code a try and post the results here. As for writing an module to override this class... I'll need to learn how to do it. Do you know any good tutorials on this subject ? again, thanks a million. Appreciate your help a lot.
    – Domeniko
    Oct 3, 2015 at 22:48
  • @user5403726: I put a short tutorial in this post: magento.stackexchange.com/questions/85143/…. Instead of rewriting a block, it will be a resource model: <models> <SwiftOtter_Test> <class>SwiftOtter_Test_Model</class> </SwiftOtter_Test> <directory_resource> <rewrite> <region_collection>MODEL_HERE</region_collection> </rewrite> </directory_resource> </models> Oct 4, 2015 at 1:02
0

The code is working all you need to do is go to admin then system-Magento cache and flush magento cache and try but the prob is that, its gonaa delete shipping and billing Regions. So i need to remove just shipping Regions.

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.