Add Company Name to customers grid
-
07-10-2020 - |
题
I need to show Company Name in customers grid. I followed exemple here: Add Company column to Customers Admin Grid with Observer and these are the files I created
Config file:
<?xml version="1.0"?>
<config>
<modules>
<Ram_CustomGrid>
<version>0.4.0</version>
</Ram_CustomGrid>
</modules>
<global>
<models>
<ram_customgrid>
<class>Ram_CustomGrid_Model</class>
</ram_customgrid>
</models>
</global>
<adminhtml>
<events>
<core_block_abstract_to_html_before>
<observers>
<beforeBlockToHtml>
<class>Ram_CustomGrid_Model_Observer</class>
<method>beforeBlockToHtml</method>
</beforeBlockToHtml>
</observers>
</core_block_abstract_to_html_before>
<eav_collection_abstract_load_before>
<observers>
<beforeBlockToHtml>
<class>Ram_CustomGrid_Model_Observer</class>
<method>beforeCollectionLoad</method>
</beforeBlockToHtml>
</observers>
</eav_collection_abstract_load_before>
</events>
</adminhtml>
</config>
Observer file:
class Ram_CustomGrid_Model_Observer
{
public function beforeBlockToHtml(Varien_Event_Observer $observer)
{
$grid = $observer->getBlock();
if ($grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
$grid->addColumnAfter('company', array(
'header' => 'Company Name',
'index' => 'company',
), 'email');
}
}
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('company');
}
}
}
While I can see Company column added to the grid, I cannot get Company Name to be displayed in table. What am I missing?
解决方案
Here is what you can do, As per this answer, you only need to add only one observer in your config.xml
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<addCompanyNameToCustomerGrid>
<class>Ram_CustomGrid_Model_Observer</class>
<method>addCompanyNameToCustomerGrid</method>
</addCompanyNameToCustomerGrid>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
Create observer with below code.
<?php
class Ram_CustomGrid_Model_Observer extends Varien_Event_Observer
{
public function addCompanyNameToCustomerGrid(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$this->_grid = $event->getBlock();
if ($this->_grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
$this->_collection = $this->_grid->getCollection();
$this->_collection->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left');
$this->_collection->addAttributeToSelect('company');
$columnData = array(
'header' => 'Company Name',
'index' => 'company',
'type' => 'text',
);
$this->_grid->addColumnAfter('company', $columnData, 'email');
$this->_grid->sortColumnsByOrder();
// rebuild the filters
$filter = $this->_grid->getParam($this->_grid->getVarNameFilter(), null);
if (is_null($filter)) {
$this->_collection->load();
}
$this->_collection->clear();
if (is_string($filter)) {
$data = $this->_grid->helper('adminhtml')->prepareFilterString($filter);
$this->_setFilterValues($data);
} else {
if ($filter && is_array($filter)) {
$this->_setFilterValues($filter);
}
}
// force a reload of the collection
$this->_collection->load();
}
}
protected function _setFilterValues($data)
{
foreach ($this->_grid->getColumns() as $columnId => $column) {
if (isset($data[$columnId]) && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0)
&& $column->getFilter()
) {
$column->getFilter()->setValue($data[$columnId]);
$this->_addColumnFilterToCollection($column);
}
}
return $this;
}
protected function _addColumnFilterToCollection($column)
{
if ($this->_collection) {
$field = ($column->getFilterIndex()) ? $column->getFilterIndex() : $column->getIndex();
if ($column->getFilterConditionCallback()) {
call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
} else {
$cond = $column->getFilter()->getCondition();
if ($field && isset($cond)) {
$this->_collection->addFieldToFilter($field, $cond);
}
}
}
return $this;
}
}