1
votes

I have created a custom module which displays a tab and a section in admin configuration panel to manage customer attributes.

I have loaded all customer attributes with a check box each.

This is my code for displaying all customer attributes as checkboxes. I want the checkbox value selected from here to be added as a column in Manage Customer Grid. Model/Attributes.php

$attributes =   Mage::getModel('customer/entity_address_attribute_collection');
    $result = array();
    foreach ($attributes as $attribute)
    {
        if (($label = $attribute->getFrontendLabel()))
            $result[$attribute->getId()] = $label;
    }
    $attributes1 = Mage::getModel('customer/entity_attribute_collection');
    $result1 = array();
    foreach ($attributes1 as $attribute1)
    {
        if (($label1 = $attribute1->getFrontendLabel()))
            $result1[$attribute1->getId()] = $label1;
    }
    $final = array_merge($result, $result1);

    return $final;

Now based on selection of these check boxes, I would like to add an extra column in 'Manage Customer' Grid.

I tried retrieving the value of selected checkbox but I just get the array index number.

       Mage::getStoreConfig('sectionname/groupname/fieldname');

Can some one tell me how to fetch the the selected checkbox value and add a column based on the selection represented by that checkbox?

Thanks in advance.

2
Client or server side? JQuery? More info.... - lucifurious
What do you mean by client or server side? This is in Magento back end's configuration panel. - ivn
Not exactly same. Mine is very simple which just controls the adding and removing part of columns in the grid. - ivn

2 Answers

0
votes

I would, in your module, set in config.xml that you are overwrting the Block Mage_Adminhtml_Block_Customer_Grid with your own block (which inherits from Mage_Adminhtml_Block_Customer_Grid) and in your own block make a function

protected function _prepareColumns()
{
    $this->addColumn('mycolumn', array(
        'header'    => Mage::helper('customer')->__('My Column'),
        'index'     => 'key',
    ));
    return parent::_prepareColumns();
}

Without knowing more about your data its hard to give better advice, but this should be enough to get you started.

0
votes

When you use array_merge you are destroying the correct indexes, which are supposed to be the attribute IDs. Also it's good practice to give your variables meaningful names.

$result = array();
$addressAttributes = Mage::getModel('customer/entity_address_attribute_collection');
foreach ($addressAttributes as $addressAttribute)
{
    if (($addrlabel = $addressAttribute->getFrontendLabel()))
        $result[$addressAttribute->getId()] = $addrlabel;
}
$customerAttributes = Mage::getModel('customer/entity_attribute_collection');
foreach ($customerAttributes as $customerAttribute)
{
    if (($custlabel = $customerAttribute->getFrontendLabel()))
        $result[$customerAttribute->getId()] = $custlabel;
}

return $result;

I suppose the next step is to remove the columns that your grid's parent will add, these are stored in a grid's protected _columns property. Not all columns are to be removed, such as the massaction column. Then add your chosen columns back in.

protected function _prepareColumns()
{
    parent::_prepareColumns();
    // remove the excess columns here

    $attributeIds = Mage::getStoreConfig('sectionname/groupname/fieldname');
    $attributes = Mage::getModel('eav/entity_attribute')->getCollection()
        ->addFieldToFilter('attribute_id', array('in' => $attributeIds));

    foreach ($attributes as $attribute)
    {
        $options = array();
        if ($attribute->getFrontendInput() == 'select') {
            foreach ($attribute->getSource()->getAllOptions() as $value) {
                $options[$value['value']] = $value['label'];
            }
        }
        $this->addColumn($attribute->getCode(), array(
            'index'   => $attribute->getCode(),
            'header'  => $attribute->getFrontendLabel(),
            'type'    => $attribute->getFrontendInput(),
            'options' => $options
        ));
    }

    return $this;
}

This way may lose useful formatting like column widths, etc. so a more sophisticated way would be to work out which columns are already in place and leave them, then only remove those that haven't been selected.