Home > magento, technical notes > Magento Notes — Add Address Field to Customer Export

Magento Notes — Add Address Field to Customer Export

October 2nd, 2009

Recently came across something weird when a client asked how come when they are in the admin interface: Customers -> Manage Customers, and then they click on “Export to CSV” the Address field isn’t export.  I don’t understand the logic of why the address is omitted.

Funny thing is that the fix to include the Address field isn’t straightforward and NOBODY ran into this problem, as there were no postings on Magento forum nor google search…I was surprised.  Anyways, what happens when users click on “Export to CSV” is essentially what you see in the grid below it is what gets exported, so the fix is to include the Address field in grid.  The file to modify is a core file, so you would have to update this when you do a Magento upgrade. Here’s the path to the file:

app/code/core/Mage/Adminhtml/Block/Customer/grid.php

Near line 45 you will find the below block of code, you will add the line that is in bold:

protected function _prepareCollection()
{
$collection = Mage::getResourceModel(’customer/customer_collection’)
->addNameToSelect()
->addAttributeToSelect(’email’)
->addAttributeToSelect(’created_at’)
->addAttributeToSelect(’group_id’)
->joinAttribute(’billing_street’, ‘customer_address/street’, ‘default_billing’, null, ‘left’)
->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’);

$this->setCollection($collection);

return parent::_prepareCollection();
}

Then near line 103 add a the code below in bold:

$this->addColumn(’Telephone’, array(
‘header’    => Mage::helper(’customer’)->__(’Telephone’),
‘width’     => ‘100′,
‘index’     => ‘billing_telephone’
));
$this->addColumn(’billing_address’, array(
‘header’    => Mage::helper(’customer’)->__(’Address’),
‘width’     => ‘150′,
‘index’     => ‘billing_street’
));

Now you should see Address field displayed in the grid and when export is clicked it would export the field accordingly.

Bookmark and Share


magento, technical notes

  1. October 3rd, 2009 at 01:32 | #1

    Sometimes Magento surprises me with its [strike]lack of[/strike] logic and, to be honest, you can see it quite clearly with export/import process. Good job on this one!

  1. No trackbacks yet.