1

Need to add a custom column in Customers grid from the status in the Module's Model.

i was using this code

config.xml

<adminhtml>
    <rewrite>
<customer_grid>Company_MyModule_Block_Adminhtml_Customer_Grid</customer_grid>
    </rewrite>
</adminhtml>

Company/MyModuleBlock/Adminhtml/Customer/Grid.php

public function setCollection($collection)
{
    //This is where the problem occurs
    $collection->getSelect()->joinLeft(
        array(
            'mycustomtable' => 'my_custom_table'
        ),
    'mycustomtable.customer_id = e.entity_id',
    );
    //This is where the problem occurs
    parent::setCollection($collection);
}
protected function _prepareColumns()
 {
     $this->addColumnAfter('custom_status',
         array(
             'header'=> Mage::helper('catalog')->__('Custom Status'),
             'index' => 'mytable_status',
             'align' => 'center',
             'width' => '20'
         ),
         'website_id'
     );


     return parent::_prepareColumns();
 }

The column is added and i can view it..i can populate the data from the same table(customers) but i am unable to populate data from any other table..(my_custom_table)

Thanks..

  • Please add more details about the issue you are facing. Are you able to add the column? Are you facing issue with populating data? – Jaimin Sutariya May 15 '17 at 6:26
  • The column is added and i can view it..i can populate the data from the same table(customers) but i am unable to populate data from any other table.. – Chaitanya May 15 '17 at 6:32
1

Just a side note ... as long it is not required, do not use rewrites. Adding columns can be done via events/observers ...

<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <your_observer_name>
                    <type>model</type>
                    <class>your_model/observer</class>
                    <method>appendColumnToCustomerGrid</method>
                </your_observer_name>
            </observers>
        </core_block_abstract_prepare_layout_before>
        <eav_collection_abstract_load_before>
            <observers>
                <your_observer_name>
                    <class>your_model/observer</class>
                    <method>beforeCollectionLoad</method>
                </your_observer_name>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>

Company/MyModule/Model/Observer.php

public function appendColumnToCustomerGrid(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('mytable_status', array(
            'header'=> Mage::helper('catalog')->__('Custom Status'),
            'index' => 'mytable_status',
            'align' => 'center',
            'width' => '20'
        ), 'website_id');
    }
}

used code from Jaimin Sutariya here!

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

    if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
        /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
        $collection->getSelect()->joinLeft(
            array(
                'mycustomtable' => 'my_custom_table'
            ),
            'mycustomtable.customer_id = e.entity_id',
            array('mycustomtable.mytable_status') // YOUR STATUS COLUMN
        );
    }
}
  • Trying this method but looks like there is an issue with $collection->getSelect()->joinLeft() everytime it trowing an error when ever i try to JoinLeft – Chaitanya May 15 '17 at 12:53
  • Geting an error as follows: ` Item (Mage_Customer_Model_Customer) with the same id "137" already exist ` – Chaitanya May 15 '17 at 13:05
  • Does it work with Jaimin Sutariyas code? – sv3n May 15 '17 at 13:14
  • 1
    It worked !! added ->group('e.entity_id') and its working fine ... thanks a lot for the help :) – Chaitanya May 15 '17 at 13:16
1

Try updating your setCollection()

public function setCollection($collection)
{
    //This is where the problem occurs
    $collection->getSelect()->joinLeft(
        array(
            'mycustomtable' => 'my_custom_table'
        ),
    'mycustomtable.customer_id = e.entity_id',
    array('mycustomtable.mytable_status') // YOUR STATUS COLUMN
    );
    //This is where the problem occurs
    parent::setCollection($collection);
}

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.