Magento: only existing countries in drop-down filter for "Country" column in Grid View

StackOverflow https://stackoverflow.com/questions/23540040

  •  17-07-2023
  •  | 
  •  

Вопрос

I have created a custom Grid View with the "Country" column:

    $this->addColumn('ship_country', array(
        'header' => $this->__('Country'),
        'index' => 'countrycode',
        'type'  => 'country'
    ));

The collection list which is displayed in the grid has only 2 countries: Ireland and UK.

Problem: the Filter Header for country column shows the drop-down list with all possible, over 200, countries stored inside Magento.

Filter showing all 247 Magento countries

Question: is it possible to force filter to show only Ireland and UK in the drop-down?

Like this:

Preferable filter display

Это было полезно?

Решение

I have solved this problem with the following code:

    $this->addColumn('ship_country', array(
        'header' => $this->__('Country'),
        'index' => 'countrycode',
        'type'  => 'options',
        'options' => Mage::helper('mymodule')->getCountries(),
    ));

Where Helper getCountries() method looks something like this:

public function getCountries() {
    $collection = Mage::getModel('mymodule/entity')->getCollection();
    $collection->getSelect()->group('countrycode');
    $countries = array();
    foreach($collection as $item) 
        $countries[$item->getcountrycode()] = Mage::getModel('directory/country')->load($item->getcountrycode())->getName();
    return $countries;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top