【问题标题】:Magento system.xml groups multiselectMagento system.xml 分组多选
【发布时间】:2014-10-10 09:00:56
【问题描述】:

在管理面板菜单->配置中,我有一个选项卡。 我正在使用:

<frontend_type>Multiselect</frontend_type>  
<source_model>adminhtml/system_config_source_customer_group_multiselect</source_model>

我得到了一个三个用户组,即 GENERAL、RETAIL、WHOLESELER。

问题,如何获得第四组,即NOT LOGGED IN?

【问题讨论】:

  • NOT LOGGED IN 的处理方式略有不同,例如,您无法在该组中创建客户,因此此源模型不会返回它。
  • 我不需要在这个组中创建客户,我需要多选一个未登录的组
  • 重点是,您正在使用的源模型在客户创建选项卡中使用,其中不需要 NOT LOGGED IN 组,因此此源模型不包括该组。我不知道其他拥有它的源模型,您必须创建自己的或手动添加该组。
  • 谢谢,但您知道如何手动添加该组吗?

标签: php magento


【解决方案1】:

您可以在 system.xml 中定义自己的观察者,而不是调用核心模型

请在下面找到可以解决您问题的代码。

 <source_model>adminhtml/system_config_source_GroupCollection</source_model>

现在在您的本地\社区(工作目录)中创建您的 GroupCollection.php 文件。

e.g app\code\local\Mage\Adminhtml\Model\System\Config\Source\GroupCollection.php

在该文件中添加以下代码。

<?php
class Mage_Adminhtml_Model_System_Config_Source_GroupCollection
{
 /**
     * Options getter
     *
     * @return array
     */
     public function toOptionArray()
    {
        $group = Mage::getModel('customer/group')->getCollection();
        $groupArray = array();
        foreach ($group as $eachGroup) {
        $groupData = array(
                    'customer_group_id' => $eachGroup->getCustomerGroupId(),
                    'customer_group_code' => $eachGroup->getCustomerGroupCode(),
                     'tax_class_id' => $eachGroup->getTaxClassId() // we dont required this
                    );
        if (!empty($groupData)) {
            array_push($groupArray, $groupData);
        }
       }
          var_dump($groupArray);
    }
}

以下将是您的输出。

array (size=4)
  0 => 
    array (size=3)
      'customer_group_id' => string '0' (length=1)
      'customer_group_code' => string 'NOT LOGGED IN' (length=13)
      'tax_class_id' => string '3' (length=1)
  1 => 
    array (size=3)
      'customer_group_id' => string '1' (length=1)
      'customer_group_code' => string 'General' (length=7)
      'tax_class_id' => string '3' (length=1)
  2 => 
    array (size=3)
      'customer_group_id' => string '2' (length=1)
      'customer_group_code' => string 'Wholesale' (length=9)
      'tax_class_id' => string '3' (length=1)
  3 => 
    array (size=3)
      'customer_group_id' => string '3' (length=1)
      'customer_group_code' => string 'Retailer' (length=8)
      'tax_class_id' => string '3' (length=1)

你就完成了! :)

【讨论】:

  • 谢谢,我试着照你说的做,但它不完全是我需要的,我像这样重写核心模块,public function toOptionArray() { if (!$this->_options) { $this->_options = Mage::getResourceModel('customer/group_collection') ->loadData()->toOptionArray(); } 返回 $this->_options;刚刚删除了这个:->setRealGroupsFilter() from code
  • 没有你我不会想到!
【解决方案2】:

您不需要在“本地”创建“法师”目录或重写核心模块!如果你查看 app/code/core/Mage/Adminhtml/Model/System/Config/Source/Customer/Group/Multiselect.php,你会发现这个类没有扩展任何东西!因此,您可以在自己的文件夹 NameSpace/ModuleName/Model 中创建自己的 Exemple.php,然后从 Multiselect 中复制 all .php 到 Exemple.php,在那里修复你想要的(在你的情况下,只需删除 -&gt;setRealGroupsFilter() 字符串),然后在 system.xml 中设置你的模型。 例如,如果在您的 config.xml 中有

<models>
    <my_model>
        <class>NameSpace_ModuleName_Model</class>
    </my_model>
</models>

比你必须写的

<source_model>my_model/exemple</source_model>

在您的 system.xml 中!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多