如何在系统配置XML文件中包含NOT LOGGED IN客户组?

时间:2016-06-24 06:34:25

标签: php magento

我已将customer列的xml标记包含在我的system.xml文件中。

>>> from itertools import count
>>> numbers = ['One', 'Two', 'Three', 'Four', 'Five']
>>> for i, number in enumerate(numbers):
...     print(i * 2, number)
...
0 One
2 Two
4 Three
6 Four
8 Five
>>> for i, number in zip(count(step=2), numbers):
...     print(i, number)
...
0 One
2 Two
4 Three
6 Four
8 Five

但NOT LOGGED IN组不再可用于多选和保存配置。 感谢有人通过在Magento中添加自定义source_model来帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

发生这种情况的原因是模型Mage_Adminhtml_Model_System_Config_Source_Customer_Group::toOptionArray告诉它不要返回" NOT LOGGED IN"客户群通过将其过滤出集合:

$this->_options = Mage::getResourceModel('customer/group_collection')
    ->setRealGroupsFilter()
    ->loadData()->toOptionArray();

检查Mage_Customer_Model_Resource_Group_Collection::setRealGroupsFilter,您会看到:

public function setRealGroupsFilter()
{
    return $this->addFieldToFilter('customer_group_id', array('gt' => 0));
}

你能做什么

如果您想要它,您需要使用自定义源模型添加它:

# File: app/code/local/You/Yourpackage/Model/System/Config/Source/Customer/Group.php
class You_Yourpackage_Model_System_Config_Source_Customer_Group extends Mage_Adminhtml_Model_System_Config_Source_Customer_Group
{
    public function toOptionArray()
    {
        if (!$this->_options) {
    $this->_options = Mage::getResourceModel('customer/group_collection')
        ->loadData()->toOptionArray();
            array_unshift($this->_options, array('value'=> '', 'label'=> Mage::helper('adminhtml')->__('-- Please Select --')));
        }
        return $this->_options;
    }
}

您还需要在config.xml中定义模型别名:

# File: app/code/local/You/Yourpackage/etc/config.xml
<global>
    <models>
        <yourpackage>
            <class>You_Yourpackage_Model</class>
        </yourpackage>
    </models>
</global>

最后,在配置XML中使用您自己的命名空间作为源模型:

<customer_group translate="label">
    <label>Customer Group</label>
    <frontend_type>multiselect</frontend_type>
    <source_model>yourpackage/system_config_source_customer_group</source_model>

请注意:我故意省略了整个模块的配置和设置,因为这个问题并非如此。