Magento国家重新订购

时间:2010-12-17 18:59:11

标签: magento magento-1.4

如何在Magento中重新排序国家默认下拉顺序。就像美国应该首先在选择选项列表中而不是按字母排序。

我无法在数据库或xml,csv,php文件中找到这些国家/地区列表存储的文件。请帮我解决这个问题。

4 个答案:

答案 0 :(得分: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;
    }

}

之后,所有国家/地区列表将首先拉美国,然后按字母顺序排列所有其他国家/地区。

答案 1 :(得分:2)

我认为在SUPEE-6788补丁解决可能的SQL注入APPSEC-1063报价后,Alex B解决方案不再起作用了。

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

我最终使用Zend_Db_Expr设置了收集顺序,并在受保护的方法中删除了Mage :: helper('core / string') - &gt; 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;
    }

}

答案 2 :(得分:1)

虽然我已经考虑过了,但我没有这样做过。我猜你可以覆盖控制器从数据库中提取数据的块。选择框只是值的数组,因此您可以调用获取国家/地区值数组的函数,使用php“手动”重新排序它们,然后将新排序的数组分配给“字段”对象。

答案 3 :(得分:1)

我也在寻找相同的内容answer 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;
}
相关问题