Engine23
blogBannerImage
By Engine23

Magento - How to add customer default shipping address to adminhtml grid

I just was asked to update/change the Admin Customer Grid to show the default Shipping information.

So the new grid has  Email, Company Name ( custom attribute ),  Customer Name, Address, City, State, Postcode, Country, Phone, Group, Customer Since and Website.

First thing we need to create the module to handle this.  This is new module is called Dpc.

app/etc/modules/Dpc_Adminhtml.xml

<?xml version="1.0"?>
<config>
<modules>
<Dpc_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
</depends>
</Dpc_Adminhtml>
</modules>
</config>

Then the app/code/local/Dpc/etc/config.xml

<?xml version="1.0" ?>
<config>
<modules>
<Dpc_Adminhtml>
<version>0.0.1</version>
</Dpc_Adminhtml>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>Dpc_Adminhtml_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<dpc_adminhtml>
<class>Dpc_Adminhtml_Helper</class>
</dpc_adminhtml>
</helpers>
</global>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Dpc_Adminhtml before="Mage_Adminhtml">Dpc_Adminhtml</Dpc_Adminhtml>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>

Now We need to create the app/code/local/Dpc/Adminhtml/Helper/Data.php

<?php
class Dpc_Adminhtml_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Now lets build the Block responsible for the new columns found in app/code/local/Dpc/Adminhtml/Block/Customer/Grid.php

<?php
class Dpc_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

/**
*
*/
public function __construct()
{
parent::__construct();
$this->setId('customerGrid');
$this->setUseAjax(true);
$this->setDefaultSort('entity_id');
$this->setSaveParametersInSession(true);
}

/**
* @return Mage_Adminhtml_Block_Widget_Grid
*/
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('company_name')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
//->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
//->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
//->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
//->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
//->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')
->joinAttribute('shipping_postcode', 'customer_address/postcode', 'default_shipping', null, 'left')
->joinAttribute('shipping_city', 'customer_address/city', 'default_shipping', null, 'left')
->joinAttribute('shipping_region', 'customer_address/region', 'default_shipping', null, 'left')
->joinAttribute('shipping_street', 'customer_address/street', 'default_shipping', null, 'left')
->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'left')
->joinAttribute('shipping_telephone', 'customer_address/telephone', 'default_shipping', null, 'left');

$this->setCollection($collection);

return parent::_prepareCollection();
}

/**
* @return $this
* @throws Exception
*/
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
/*$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
$this->addColumn('lastname', array(
'header' => Mage::helper('customer')->__('Last Name'),
'index' => 'lastname'
));*/
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$this->addColumn('company_name', array(
'header' => Mage::helper('customer')->__('Company Name'),
'index' => 'company_name'
));
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));

$this->addColumn('shipping_street', array(
'header' => Mage::helper('customer')->__('Street Address'),
'width' => '150',
'index' => 'shipping_street',
));

$this->addColumn('shipping_city', array(
'header' => Mage::helper('customer')->__('City'),
'width' => '100',
'index' => 'shipping_city',
));
$this->addColumn('shipping_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'shipping_region',
));

$this->addColumn('shipping_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'shipping_postcode',
));
$this->addColumn('shipping_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'shipping_country_id',
));

$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'shipping_telephone'
));

$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt'=> 0))
->load()
->toOptionHash();

$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));

$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));

if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('customer')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}

$this->addColumn('action',
array(
'header' => Mage::helper('customer')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('customer')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));


$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
return parent::_prepareColumns();
}

/**
* @return $this
*/
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('customer');

$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('customer')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('customer')->__('Are you sure?')
));

$this->getMassactionBlock()->addItem('newsletter_subscribe', array(
'label' => Mage::helper('customer')->__('Subscribe to Newsletter'),
'url' => $this->getUrl('*/*/massSubscribe')
));

$this->getMassactionBlock()->addItem('newsletter_unsubscribe', array(
'label' => Mage::helper('customer')->__('Unsubscribe from Newsletter'),
'url' => $this->getUrl('*/*/massUnsubscribe')
));

$groups = $this->helper('customer')->getGroups()->toOptionArray();

array_unshift($groups, array('label'=> '', 'value'=> ''));
$this->getMassactionBlock()->addItem('assign_group', array(
'label' => Mage::helper('customer')->__('Assign a Customer Group'),
'url' => $this->getUrl('*/*/massAssignGroup'),
'additional' => array(
'visibility' => array(
'name' => 'group',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $groups
)
)
));

return $this;
}


}

Refresh your caches and you should have some new columns.