Friday, April 6, 2012

How to show cutomer street address in Admin Grid like street1 and Street2 :Solved

Hi
I search everywhere to show the street address in customer grid in admin end But no found any solution for this.
Finally I review the code and make a new way to do this .

1. copy file Grid.php form ...app\code\core\Mage\Adminhtml\Block\Customer\Grid.php  to .....app\code\local\Mage\Adminhtml\Block\Customer\Grid.php



2. open the customer 's Grid.php in ....app\code\local\Mage\Adminhtml\Block\Customer\Grid.php

and add the follwing code in _prepareColumns() function

\\\\\\\\\\\\\\\\\\\\\


 $this->addColumn('street1', array(
          'header'    => Mage::helper('customer')->__('Street 1'),
          'index'     => 'entity_id',
 'renderer'=> new Mage_Adminhtml_Block_Customer_Renderer_Street1()
         ));
$this->addColumn('street2', array(
          'header'    => Mage::helper('customer')->__('Street 2'),
          'index'     => 'entity_id',
 'renderer'=> new Mage_Adminhtml_Block_Customer_Renderer_Street2()
         ));


\\\\\\\\\\\\\\\\\\\\\\\

2. make a directory in  ....app\code\local\Mage\Adminhtml\Block\Customer\     named  Renderer

and create two files in  ....app\code\local\Mage\Adminhtml\Block\Customer\Renderer\

Street1.php
for code
\\\\\\\\\\\\\\\\

class Mage_Adminhtml_Block_Customer_Renderer_Street1 extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

public function render(Varien_Object $row)
{
$value =  $row->getData($this->getColumn()->getIndex());

 $customer = Mage::getModel('customer/customer')
 ->load($value); //put customer id here

$data = array();

 foreach ($customer->getAddresses() as $address)
 {

    $data = $address->getStreet(1);

 

 }
 if(empty($data))
 {
 return '';
 }else{
 return $data;
 }



}

}


\\\\\\\\\\\\\\\\\

and second one is Street2.php

\\\\\\\\\\\\\\\\\\\\\\\\\\\\

class Mage_Adminhtml_Block_Customer_Renderer_Street2 extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

public function render(Varien_Object $row)
{
$value =  $row->getData($this->getColumn()->getIndex());

 $customer = Mage::getModel('customer/customer')
 ->load($value); //put customer id here

$data = array();

 foreach ($customer->getAddresses() as $address)
 {

    $data = $address->getStreet(2);

    //var_dump($data);

 }
if(empty($data))
 {
 return '';
 }else{
 return $data;
 }

}

}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


and that all done.


Enjoy!!

7 comments:

  1. Really its worked...awesome work by Vinod..keep it up dude..

    ReplyDelete
  2. Great job! Your blog is provide very nice information. Thanks for sharing this.
    IT Company India

    ReplyDelete
  3. How can is store two different street1 and street2 with code

    ReplyDelete
    Replies
    1. Hi Hiren Soni,

      you can try like following......
      ////////////
      $customer=Mage::getModel('customer/address')->load('customer_id');

      $customer->setData('street',array('1'=>'abc','2'=>'xyz'));
      $customer->save();
      ////////

      Delete
  4. Thank you Thank you Thank you!

    ReplyDelete
  5. If show the listing thanks for that but search filters not working can you tell me for filters what i have to do more

    ReplyDelete