In your custom module database has the customer_id field:
IN Grid.php
protected function _prepareCollection() { $collection = Mage::getModel('custommodule/custommodule')->getCollection(); $customerFirstNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('firstname'); $customerLastNameAttr = Mage::getSingleton('customer/customer')->getResource()->getAttribute('lastname'); $collection->getSelect() ->joinLeft( array('cusFirstnameTb' => $customerFirstNameAttr->getBackend()->getTable()), 'main_table.customer_id = cusFirstnameTb.entity_id AND cusFirstnameTb.attribute_id = '.$customerFirstNameAttr->getId(). ' AND cusFirstnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(), array('cusFirstnameTb.value') ); $collection->getSelect() ->joinLeft( array('cusLastnameTb' => $customerLastNameAttr->getBackend()->getTable()), 'main_table.customer_id = cusLastnameTb.entity_id AND cusLastnameTb.attribute_id = '.$customerLastNameAttr->getId(). ' AND cusLastnameTb.entity_type_id = '.Mage::getSingleton('customer/customer')->getResource()->getTypeId(), array('customer_name' => "CONCAT(cusFirstnameTb.value, ' ', cusLastnameTb.value)") ); $this->setCollection($collection); return parent::_prepareCollection(); }
protected function _prepareColumns() { //------------- Other Fields --------- $this->addColumn('customer_id', array( 'header' => Mage::helper('serialcodes')->__('Customer Name'), 'align' => 'left', 'type' => 'text', 'index' => 'customer_id', 'filter_condition_callback' => array($this, '_customerNameFilter'), 'renderer' => 'Namespace_YourModule_Block_Adminhtml_YourModule_Renderer_Customer', )); $this->addExportType('*/*/exportCsv', Mage::helper('serialcodes')->__('CSV')); $this->addExportType('*/*/exportXml', Mage::helper('serialcodes')->__('XML')); return parent::_prepareColumns(); }
Create a function in Grid.php after _prepareColumns() function:
public function _customerNameFilter($collection, $column) { $filterValue = $column->getFilter()->getValue(); if(!is_null($filterValue)){ $filterValue = trim($filterValue); $filterValue = preg_replace('/[\s]+/', ' ', $filterValue); $whereArr = array(); $whereArr[] = $collection->getConnection()->quoteInto("cusFirstnameTb.value like ?", "%$filterValue%"); $whereArr[] = $collection->getConnection()->quoteInto("cusLastnameTb.value like ?", "%$filterValue%"); $whereArr[] = $collection->getConnection()->quoteInto("CONCAT(cusFirstnameTb.value, ' ', cusLastnameTb.value) like ?", "%$filterValue%"); $where = implode(' OR ', $whereArr); $collection->getSelect()->where($where); } }
create a renderer file in app/code/local/Namespace/YourModule/Block/Adminhtml/YourModule/Renderer/Customer.php
with the following content:
<?php class Namespace_YourModule_Block_Adminhtml_YourModule_Renderer_Customer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { $customerId = $row->getData($this->getColumn()->getIndex()); $customerData = Mage::getModel('customer/customer')->load($customerId); $customerName = $customerData->getFirstname() . ' ' . $customerData->getLastname(); if($customerId){ return "<a href='".$this->getUrl('adminhtml/customer/edit',array('id'=>$customerId))."'>$customerName</a>"; }else{ return false; } } }
0 Comments