2010-12-17 84 views
1

我如何重新訂購Magento中的國家/地區默認下拉訂單。像美國應該首先在選擇選項列表中,而不是按字母排序。Magento國家重新訂購

我無法在數據庫或xml,csv,php文件中找到這些國家/地區列表存儲的位置。請幫我解決這個問題。

回答

1

我還沒有這樣做,但我已經考慮過了。我猜你可以重寫控制器從數據庫中提取數據的塊。選擇框只是值的數組,所以也許你可以調用獲取國家數值數組的函數,使用php「手動」對它們重新排序,然後將新排序的數組分配給「字段」對象。

+0

這似乎是最有可能的解決方案,因爲他們都存儲在表('directory_country')並沒有像大多數使用的其它表的特定排序字段。 – clockworkgeek 2010-12-19 18:27:53

+0

感謝您的回覆@Chris。我是這麼想的,但是我一直在尋找像magento這樣的排序方式,好的,讓我試試看。 – Elamurugan 2010-12-20 08:59:38

3

我一直在尋找相同的東西,最終自己做了。決定張貼在這裏,希望這可以幫助別人。

創建您自己的模塊(如果它尚未存在)。重寫Mage_Directory_Model_Resource_Country_Collection類:

<config> 
    <modules> 
     <Your_Module> 
      <version>0.0.0.1</version> 
     </Your_Module> 
    </modules> 
    <global> 
     <models> 
      <directory_resource> 
       <rewrite> 
        <country_collection>Your_Module_Model_Resource_Directory_Country_Collection</country_collection> 
       </rewrite> 
      </directory_resource> 
     </models> 
    </global> 
</config> 

在你的Magento根目錄創建以下文件:

/app/code/local/Your/Module/Model/Resource/Directory/Country/Collection.php 

具有以下內容:

<?php 

class Your_Module_Model_Resource_Directory_Country_Collection 
    extends Mage_Directory_Model_Resource_Country_Collection 
{ 

    protected function _initSelect() 
    { 
     parent::_initSelect(); 

     $this 
      ->addOrder('country_id="US"','desc') 
      ->addOrder('country_id', 'asc'); 

     return $this; 
    } 

} 

之後,所有的國家名單將拉動美國首先,然後按字母順序排列所有其他國家。

+0

它沒有按預期工作。國家的秩序仍然是按字母順序排列的。沒有**美國**在頂部 – 2014-03-13 13:16:28

0

我也在尋找相同的和answerAlex B不在我的情況下工作。此外代碼

$this->addOrder('country_id="US"','desc') 
    ->addOrder('country_id', 'asc'); 

在國家列表中沒有任何更改。所以,而不是重寫_initSelect()我選擇toOptionArray()覆蓋。這裏是我的代碼

public function toOptionArray($emptyLabel = '') { 
    $options = parent::toOptionArray($emptyLabel); 
    foreach ($options as $key => $option) { 
     if ($option['value'] == 'IN') { //check current country code 
      unset($options[$key]); 
      $options = addCountryToIndex($options, $option, 1); 
     } 
    } 
    return $options; 
} 

protected function addCountryToIndex($countries, $country, $index){ 
    $options = array_slice($countries, 0, $index, true) + 
     array($index => $country) + 
     array_slice($countries, $index, count($countries) - 1, true); 
    return $options; 
} 
1

我認爲Alex B解決方案並不SUPEE-6788的補丁解決可能的SQL注入APPSEC-1063報價逃脫後不再工作。

Mohammad Faisal覆蓋導致一些國家在我們的商店啓用Tablerates後消失。

我最終設置了Zend_Db_Expr的集合順序,並在受保護的方法中移除了Mage :: helper('core/string') - > ksortMultibyte($ sort)。

在Magento 1.9.3.2中測試。

<?php 

class Your_Module_Model_Resource_Directory_Country_Collection extends Mage_Directory_Model_Resource_Country_Collection { 

/** 
* Convert items array to array for select options pushing most important countries to the top 
* 
* return items array 
* array(
*  $index => array(
*   'value' => mixed 
*   'label' => mixed 
*  ) 
*) 
* 
* @param string $valueField 
* @param string $labelField 
* @return array 
*/ 
protected function _toOptionArray($valueField = 'id', $labelField = 'name', $additional = array()) 
{ 
    $res = array(); 
    $additional['value'] = $valueField; 
    $additional['label'] = $labelField; 

    $this->getSelect()->order(new Zend_Db_Expr('country_id = "NL" DESC')); 
    $this->getSelect()->order(new Zend_Db_Expr('country_id = "BE" DESC')); 
    $this->getSelect()->order(new Zend_Db_Expr('country_id = "DE" DESC')); 
    $this->getSelect()->order(new Zend_Db_Expr('country_id = "FR" DESC')); 
    $this->getSelect()->order('country_id', 'DESC'); 

    foreach ($this as $item) { 
     foreach ($additional as $code => $field) { 
      $data[$code] = $item->getData($field); 
     } 
     $res[] = $data; 
    } 

    return $res; 
} 

/** 
* Convert collection items to select options array 
* 
* @param string $emptyLabel 
* @return array 
*/ 
public function toOptionArray($emptyLabel = ' ') 
{ 
    $options = $this->_toOptionArray('country_id', 'name', array('title' => 'iso2_code')); 

    $sort = array(); 
    foreach ($options as $data) { 
     $name = Mage::app()->getLocale()->getCountryTranslation($data['value']); 
     if (!empty($name)) { 
      $sort[$name] = $data['value']; 
     } 
    } 

    // Mage::helper('core/string')->ksortMultibyte($sort); 
    $options = array(); 
    foreach ($sort as $label => $value) { 
     $options[] = array(
      'value' => $value, 
      'label' => $label 
     ); 
    } 

    if (count($options) > 0 && $emptyLabel !== false) { 
     array_unshift($options, array('value' => '', 'label' => $emptyLabel)); 
    } 

    return $options; 
    } 

}