3

I've successfully added a customer attribute that records the branche in which the customer is active. I need this value to be shown in the customer grid so the client is able to instantly obtain this piece of information. What I've done so far:

  1. Updated my custom module config.xml file with an Observer:

    <adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <customgrid_column_append>
                    <type>model</type>
                    <class>MyCompany_ModuleName_Model_Observer</class>
                    <method>appendCustomColumn</method>
                </customgrid_column_append>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
    </adminhtml>
    
  2. Added the actual observer

    <?php 
    class MyCompany_MyModule_Model_Observer extends                      Varien_Event_Observer
    {
    
    public function appendCustomColumn(Varien_Event_Observer $observer)
    {
    $block = $observer->getBlock();
    if (!isset($block)) {
        return $this;
    }
    
    if ($block->getType() == 'adminhtml/customer_grid') {
        /* @var $block Mage_Adminhtml_Block_Customer_Grid */
        $block->addColumnAfter('branche_customer', array(
            'header'    => 'Branche',
            'type'      => 'text',
            'index'     => 'branche_customer',
        ), 'name');
     }
     }
     }
    

So far so good. When I run the customer grid in the backend the column is added after the "name" column. However no results are returned in the Branche column. The attribute Id is correct. The attribute itself is a dropdown field.

What am I missing here?

3

There is a great answer for this already with two interesting solutions: https://magento.stackexchange.com/a/5975/5913

Basically you can

Try to hook to this events:

<adminhtml>
    <events>
        <core_block_abstract_to_html_before>
            <observers>
                <{observer_name}>
                    <type>singleton</type>
                    <class>{namespace}_{module}/observer</class>
                    <method>beforeBlockToHtml</method>
                </{observer_name}>
            </observers>
        </core_block_abstract_to_html_before>
        <eav_collection_abstract_load_before>
            <observers>
                <{observer_name}>
                    <class>{namespace}_{module}/observer</class>
                    <method>beforeCollectionLoad</method>
                </{observer_name}>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>

And then in the Observer:

public function beforeBlockToHtml(Varien_Event_Observer $observer)
{
    $grid = $observer->getBlock();

    /**
     * Mage_Adminhtml_Block_Customer_Grid
     */
    if ($grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
        $grid->addColumnAfter(
            '{column_code}',
            array(
                'header' => Mage::helper('{Module}_customer')->__('{{column_name}}'),
                'index'  => '{column_code}'
            ),
            'entity_id'
        );
    }
}

public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    if (!isset($collection)) {
        return;
    }

    /**
     * Mage_Customer_Model_Resource_Customer_Collection
     */
    if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
        /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
        $collection->addAttributeToSelect('{attribute_code}');
    }
}

Or you can try override two methods in Mage_Adminhtml_Block_Customer_Grid:

_prepareCollection - to add your attribute in the collection
_prepareColumns - to add the column in your grid.

In your config.xml add this:

    <blocks>
        <adminhtml>
            <rewrite>
                <customer_grid>Company_Customer_Block_Adminhtml_Customer_Grid</customer_grid>
            </rewrite>
        </adminhtml>
    </blocks>

Then you need to add this class: Company_Customer_Block_Adminhtml_Customer_Grid that should extend Mage_Adminhtml_Block_Customer_Grid.

3
  • I've looked at both solutions and the first seemed the most appropriate one considering the success I've had so far. I once again tried the solution you suggested but I guess I'm missing some knowledge on how to implement this properly. Let's take the following information and you might be able to enlighten me to construct this correctly: Namespace & Module > Testcomp // Branchecustom // Attribute: branche_customer – Badger Jun 2 '15 at 15:35
  • 1
    I would personally go for the second solution. Less override, less problems. – Mayers Jun 2 '15 at 15:39
  • 1
    Once again used the second option and this time it did work! Guess I had a type somewhere that prevented the grid from rendering. – Badger Jun 2 '15 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.