Deepak Mankotia
Deepak Mankotia

Reputation: 4564

How to add new attributes in Cutstomer Export CSV file in magento

I want to add new attributes in Customer Export CSV file. My fields are date of birth and Gender.

I have added the below code in code/core/Mage/Adminhtml/Block/Customer/Grid.php

$this->addColumn('dob', array(
        'header'    => Mage::helper('customer')->__('Date of Birth'),
        'type'      => 'datetime',
        'align'     => 'center',
        'index'     => 'dob',
        'gmtoffset' => true
    ));
$this->addColumn('gender', array(
        'header'    => Mage::helper('customer')->__('Gender'),
        'index'     => 'gender'
    ));

In admin panel under manage customer it shows new files with same name but empty data and also in CSV file it added the title for both the fields but fields are empty.

How can i add new fields in customer export file in magento?

Upvotes: 1

Views: 2195

Answers (1)

Marko Krstic
Marko Krstic

Reputation: 1447

instead of your gender code, put this:

$genders = $this->_getGenderOptions();
$this->addColumn('gender', array(
    'header'    => Mage::helper('customer')->__('Gender'),
    'index'     => 'gender',
    'type'      =>  'options',
    'options'   =>  $genders
));

then, in that Grid class create new method:

private function _getGenderOptions()
{
    $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();
    $array = array();

    foreach ($options as $option)
    {
        if($option['label'] == "")
            continue;

        $array[$option['value']] = $option['label'];
    }

    return $array;
}

and in the method

protected function _prepareCollection();

add by loading the collection:

->addAttributeToSelect('dob')
->addAttributeToSelect('gender')

it should look like:

$collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('dob') // new code
        ->addAttributeToSelect('gender') // new code
        ->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');

and that's it!

Upvotes: 1

Related Questions