0

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 :-)

2 Answers 2

1

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

6
  • Thanks, I've done this but when I try to run a CSV export I get the following error: Cannot send headers; headers already sent in /.../Renderer/FirstName.php, line 1 Commented Sep 26, 2014 at 9:59
  • Please check that the file has no spaces before <?php tag
    – Amasty
    Commented Sep 26, 2014 at 10:19
  • Nope, no spaces before the PHP tag :( Commented Sep 26, 2014 at 10:38
  • I'm still having problems with this: Cannot send headers; headers already sent in /app/code/local/Mage/Adminhtml/Block/Customer/Renderer/Name.php, line 1 - any idea what would cause it? :( Commented Sep 29, 2014 at 14:53
  • can you please update your answer and post the whole file (may be as an attachment or link to download?)
    – Amasty
    Commented Sep 29, 2014 at 20:53
1

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.

17
  • I have the same problem with your code as I do below: Cannot send headers; headers already sent in /.../Renderer/FirstName.php, line 1 Commented Sep 26, 2014 at 11:49
  • I think check the render file path problem
    – Keyul Shah
    Commented Sep 26, 2014 at 12:05
  • What should the Render filer path be? I originally had it here: app/code/local/Mage/Adminhtml/Block/Customer/Renderer Commented Sep 26, 2014 at 13:01
  • The code works - It only errors when running an Export Commented Sep 26, 2014 at 13:01
  • whar error you got?
    – Keyul Shah
    Commented Sep 26, 2014 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.