0

I am new magento and I want to override my "Data.php" file which is located at /app/code/core/Mage/Directory/Helper/Data.php

I want to remove the unwanted states from DropDownList.

Below is my Function in Data.php file

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

        foreach ($collection as $region) {
            if (!$region->getRegionId()) {
                continue;
            }

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


In the above function I want to add code for removing the unwanted states from dropdown. I have added the code for this but when I am adding in the above function I am not getting any effect.


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;
            }

             //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;
    }

Even the whole Data.php file along with the above change i kept in local directory but it didn't work.

0

I don't encourage you to use this way of override, like creating app/code/local/Mage/Directory/Helper/Data.php, this is just the good way for spaghetti code..

  1. Create a module Vids_Directory in pool local like app/code/local/Vids/Directory
  2. Create app/etc/modules/Vids_Directory.xml and this code :

    <?xml version="1.0"?>
    <config>
        <modules>
            <Vids_Directory>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Directory />
                </depends>
            </Vids_Directory>
        </modules>
    </config>
    
  3. Create app/code/local/Vids/Directory/etc/config.xml and add this code :

    <?xml version="1.0"?>
    <config>
        <modules>
            <Vids_Directory>
                <version>0.1.0</version>
            </Vids_Directory>
        </modules>
        <global>
            <helpers>
                <directory>
                    <rewrite>
                        <data>Vids_Directory_Helper_Data</data>
                    </rewrite>
                <directory>
            </helpers>
        </global>
    </config>
    
  4. Finally, you can create app/code/local/Vids/Helper/Data.php and add this :

    <?php
    
    class Vids_Directory_Helper_Data extends Mage_Directory_Helper_Data 
    {
        protected function _getRegions($storeId) {
            // Your code inside
        }
    }
    

Note : your code $this->_select->from... is referring to nothing, be careful (or probably you missed to add your complete code ?).

Hope it will help you :).

  • Thanks for your help. I tried this but my page did not loaded at all .Only the website symbol is displayed.And can you tell me how should I edit this code "$this->_select->from..."? – vids Jul 11 '16 at 18:15
  • If you to add more filter on region collection, you can use the function addFieldToFilter() and the condition nin in your case. For example : $collection->addFieldToFilter('region_code', array('nin', array('AE', 'AA')));. You can do this when Magento is creating the $collectionobject ;) – TaKe_Da_ShAkEr Jul 11 '16 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.