Add column in Customer grid based on module's Model
-
08-10-2020 - |
Вопрос
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..
Решение
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
);
}
}
Другие советы
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);
}
Не связан с magento.stackexchange