如何在 Magento 中重新排序国家默认下拉顺序。像美国应该在选择选项列表中排在第一位而不是按字母排序。
我无法在数据库或 xml、csv、php 文件中找到这些国家/地区列表存储的位置。请帮我解决这个问题。
如何在 Magento 中重新排序国家默认下拉顺序。像美国应该在选择选项列表中排在第一位而不是按字母排序。
我无法在数据库或 xml、csv、php 文件中找到这些国家/地区列表存储的位置。请帮我解决这个问题。
我一直在寻找相同的东西,最终自己做了。决定在这里发布,希望这对其他人有帮助。
创建您自己的模块(如果尚不存在)。重写 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;
}
}
之后,所有国家列表将首先拉美国,然后按字母顺序拉所有其他国家。
我认为在 SUPEE-6788 补丁解决可能的 SQL 注入 APPSEC-1063 引号转义后, Alex B解决方案不再起作用。
在我们的商店启用 Tablerates 后,Mohammad Faisal覆盖导致一些国家消失。
我最终用 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;
}
}
我没有这样做,尽管我已经考虑过了。我猜你可以覆盖控制器从数据库中提取数据的块。选择框只是值的数组,所以也许您可以调用获取国家值数组的函数,使用 php“手动”重新排序它们,然后将新排序的数组分配给“字段”对象。
我也在寻找相同的答案,而Alex 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;
}