Shabinraj
Shabinraj

Reputation: 31

How can I include NOT LOGGED IN customer group in my system configuration XML file?

I have included the xml tag for customer group as below in my system.xml file.

<customer_group translate="label">
    <label>Customer Group</label>
    <frontend_type>multiselect</frontend_type>
    <source_model>adminhtml/system_config_source_customer_group</source_model>
    <sort_order>30</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <comment><![CDATA[Specific customer group selection.]]></comment>
</customer_group>

But NOT LOGGED IN group is no longer available for multi selection and saving for the configuration. I appreciate somebody to help me on resolving this issue by adding custom source_model in Magento.

Upvotes: 2

Views: 289

Answers (1)

scrowler
scrowler

Reputation: 24405

The reason this happens is that the model Mage_Adminhtml_Model_System_Config_Source_Customer_Group::toOptionArray tells it not to return the "NOT LOGGED IN" customer group by filtering it out of the collection:

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

Examine Mage_Customer_Model_Resource_Group_Collection::setRealGroupsFilter and you see:

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

What you can do

If you want it there, you'll need to add it with a custom source model:

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

You'll also need to define a model alias in your config.xml:

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

Finally, use your own namespace for the source model in your config XML:

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

Please note: I deliberately left out the entirety of the module configuration and setup, because this question isn't about that.

Upvotes: 1

Related Questions