Remove unwanted states from dropdown list in Magento

时间:2016-07-11 19:05:28

标签: php mysql wordpress magento

I am new to magento and I am not able to make out how one can remove the unwanted states from dropdown list in magento. I tried to edit my below function as per this link http://doejo.com/blog/magento-how-to-remove-certain-states-from-state-region-list-at-checkout-or-registration/ but it did not work. Below i sthe code for removing unwanted states but when I add to my class in local folder, not getting any results.


    $excludeRegions = array('AE','AA');
            foreach ($collection as $region) {
                if (!$region->getRegionId()) {
                    continue;
                }
                ***//Here is the code from the link given
                 //BOF Custom Logic Here***
                $regionCode = $region->getCode();
                $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);

_getRegions function edited as per above code and link respectively.

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('AE','AA');
        foreach ($collection as $region) {
            if (!$region->getRegionId()) {
                continue;
            }
            ***//Here is the code from the link given
             //BOF Custom Logic Here***
            $regionCode = $region->getCode();
            $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);

            //EOF Custom Logic Here
            $regions[$region->getCountryId()][$region->getRegionId()] = array(
                'code' => $region->getCode(),
                'name' => $this->__($region->getName())
            );
        }
        return $regions;
    }

After making the above changes, I tried below steps as my code is in /app/code/core/Mage/Directory/Helper/Data.php

  1. Create a module Vids_Directory in pool local like app/code/local/Vids/Directory
  2. Create app/etc/modules/Vids_Directory.xml
  3. Create app/code/local/Vids/Directory/etc/config.xml
  4. create app/code/local/Vids/Helper/Data.php

1 个答案:

答案 0 :(得分:1)

从Region下拉列表中删除不需要的状态的一种更简单但不推荐的方法是转到“directory_country_region”表并删除要删除的所有States ID。更好的方法是进行以下更改。

  1. 覆盖Mage_Directory_Helper_Data类的_getRegions($ storeId)方法。
  2. 将要删除的区域代码分配给数组,即$ excludeRegions变量。
  3. 添加逻辑以从可用列表中删除上述区域代码
  4. 所以最终的代码是

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

    来源: http://www.demagento.com/magento-how-to-remove-unwanted-states-from-stateregion-list-at-checkout/