2010-12-17 99 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; 
    } 

}