【问题标题】:Magento Country ReorderMagento 国家/地区重新排序
【发布时间】:2010-12-17 18:59:11
【问题描述】:

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

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

【问题讨论】:

    标签: magento magento-1.4


    【解决方案1】:

    我一直在寻找相同的方法,最终自己完成了。决定在这里发布,希望这对其他人有帮助。

    创建您自己的模块(如果尚不存在)。重写 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;
        }
    
    }
    

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

    【讨论】:

    • 它没有按预期工作。国家的顺序仍然是按字母顺序排列的。顶部没有 US
    【解决方案2】:

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

    Mohammad Faisal overwrite 在我们的商店启用 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;
        }
    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 这似乎是最有可能的解决方案,因为存储它们的表 (directory_country) 不像大多数其他正在使用的表那样具有特定的排序字段。
      • 感谢您的回复@Chris。我是这么想的,但我一直在寻找 magento 可以做到的方式,比如排序顺序,好吧,让我试试吧。
      【解决方案4】:

      我也在寻找相同的东西,而 Alex B 的另一个 answer 在我的情况下不起作用。还有代码

      $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;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 2018-09-15
        相关资源
        最近更新 更多