Question

I'm trying to display Customer initials instead of full names in the Customer Grid. ie. Instead of "Mr John Middle Smith" I would like "Mr J M Smith"

Can anyone point me in the right direction to change this:

    $this->addColumn('firstname', array(
        'header'    => Mage::helper('customer')->__('First Name'),
        'index'     => 'firstname',
    ));

To just get the first character of firstname

Thanks :-)

Was it helpful?

Solution

You can use your own renderer for this. See the example:

$this->addColumn('firstname', array(
        'header'    => Mage::helper('customer')->__('First Name'),
        'renderer'  => 'amexample/adminhtml_customer_grid_renderer_name',
         ....
    )); 

The renderer class:

class Amasty_Example_Block_Adminhtml_Customer_Grid_Renderer_Name extends  Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        return substr($row->getFirstname(), 0, 1);
    }
}

you can find default rendereres here:
app\code\core\Mage\Adminhtml\Block\Widget\Grid\Column\Renderer

OTHER TIPS

Step 1: You have to override the file in local folder or just copy app\code\core\Mage\Adminhtml\Block\Customer\Grid.php to app\code\local\Mage\Adminhtml\Block\Customer\Grid.php

Step 2: Replace below code with

    $this->addColumn('name', array(
        'header'    => Mage::helper('customer')->__('Name'),
        'index'     => 'name'
    ));

Replace by

     $this->addColumn('namewithprifx', array(
       'header'    => Mage::helper('customer')->__('First Name'),
        'index'     => 'firstname',
        'type'  => 'text',      
        'width' => '250px',
        'sortable'  =>false,
        'filter' => false,                                         
        'renderer' => 'NameSpace_Customergrid_Block_Adminhtml_Renderer_Namewithprifx',          
        ));

Step 3: Create file in NameSpace\Customergrid\Block\Adminhtml\Renderer\Namewithprifx.php

Step 4: Copy below code.

 <?php
 class NameSpace_Customergrid_Block_Adminhtml_Renderer_Namewithprifx extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
 {
   public function render(Varien_Object $row)
   {

    //$getData = $row->getData();               
    $str=$row->getData('prefix'). " ". $row->getData('fname'). " ". $row->getData('lname');
    return $str;
  }
}

Step 5: Refresh cache and check the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top