首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento Country重新排序

Magento Country重新排序
EN

Stack Overflow用户
提问于 2010-12-18 02:59:11
回答 4查看 2.2K关注 0票数 1

如何在Magento中重新排序国家/地区默认下拉顺序。“像美国”应该在选择选项列表中排在第一位,而不是按字母顺序排序。

我在数据库或xml、csv、php文件中都找不到这些国家列表的存储位置。请帮我解决这个问题。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-12-20 00:33:53

我没有这样做过,尽管我已经考虑过了。我猜你可以重写控制器,也就是从数据库中提取数据的块。选择框只是值的数组,所以也许你可以调用获取国家值数组的函数,用php“手动”重新排序它们,然后将新排序的数组分配给"field“对象。

票数 1
EN

Stack Overflow用户

发布于 2014-01-10 16:52:23

我也在寻找同样的东西,最终我自己完成了。决定把它贴在这里,希望这对其他人有帮助。

创建您自己的模块(如果它还不存在)。重写Mage_Directory_Model_Resource_Country_Collection类:

代码语言:javascript
复制
<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根目录中创建以下文件:

代码语言:javascript
复制
/app/code/local/Your/Module/Model/Resource/Directory/Country/Collection.php

内容如下:

代码语言:javascript
复制
<?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;
    }

}

在此之后,所有国家列表将首先拉动美国,然后按字母顺序拉出所有其他国家。

票数 3
EN

Stack Overflow用户

发布于 2017-06-22 21:32:46

我认为Alex B解决方案在SUPEE-6788修补程序解决了可能的SQL注入APPSEC-1063引号转义后不再起作用。

在我们的商店上启用Tablerate后,Mohammad Faisal覆盖导致一些国家消失。

最后,我使用Zend_Db_Expr设置了收集顺序,并删除了protected方法中的Mage::helper('core/string')->ksortMultibyte($sort)。

在Magento 1.9.3.2中测试。

代码语言:javascript
复制
<?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;
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4473880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档