2

I'm adding Custom field/Column to Customer table. but it's not showing in table as well as customer grid from admin panel.

my code is:

C:\xampp\htdocs\magento\app\code\core\Mage\Customer\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mage_Customer>
            <version>1.6.2.0.2</version>
        </Mage_Customer>
    </modules>
    <admin>
        <fieldsets>
            <customer_dataflow>
                <mobile>
                    <billing>1</billing>
                    <shipping>1</shipping>
                    <mapped>1</mapped>
                </mobile>
            </customer_dataflow>
        </fieldsets>
    </admin>
    <global>
        <fieldsets>
            <customer_account>               
                <Mobile>
                    <create>1</create>
                    <update>1</update>
                </Mobile>
            </customer_account>        
        </fieldsets>
   .....
</config>

C:\xampp\htdocs\magento\app\code\core\Mage\Customer\Model\Resource\Setup.php

<?php

class Mage_Customer_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
    ...

 public function getDefaultEntities()
    {
        $entities = array(
            'customer'                       => array(
                'entity_model'                   => 'customer/customer',
                'attribute_model'                => 'customer/attribute',
                'table'                          => 'customer/entity',
                'increment_model'                => 'eav/entity_increment_numeric',
                'additional_attribute_table'     => 'customer/eav_attribute',
                'entity_attribute_collection'    => 'customer/attribute_collection',
                'attributes'                     => array(

                    'mobile'             => array(
                        'type'               => 'int',
                        'label'              => 'mobile',
                        'input'              => 'text',
                        'required'           => false,
                        'sort_order'         => 111,
                        'position'           => 111,
                        'visible'            => true,
                    ),

                )
            ),

        );
        return $entities;
    }    
    ...       
}

C:\xampp\htdocs\magento\app\code\core\Mage\Customer\sql\customer_setup\upgrade-1.6.2.0-1.6.2.0.2.php

<?php


$installer = $this;
$installer->startSetup();

$this->addAttribute('customer', 'mobile', array(

    'type' => 'varchar',

    'input' => 'text',

    'label' => 'mobile',

    'global' => 1,

    'visible' => 1,

    'required' => 0,

    'user_defined' => 1,

    'visible_on_front' => 1

));

Mage::log('Working');

$installer->endSetup();

$customerattribute = Mage::getModel('customer/attribute')->loadByCode('customer','mobile');
$forms = array('customer_register_address',
    'customer_address_edit',
    'adminhtml_customer_address');
$customerattribute->setData('used_in_forms',$forms);

$customerattribute->save();

Could you please tell me where I went wrong?

thanks in advance.

  • 1
    You really should namespace that attribute code. I could see "mobile" as an easy collision down the road. – benmarks Apr 16 '14 at 15:57
  • @benmarks, any way working fine. but I have question regarding this. actually this field value is save on customer_entity_varchar but this Column is Where will create? – Magento Nav Apr 17 '14 at 6:45
5

Unfortunately you need to add your attribute to the grid collection and link that field to a new column in the grid block. Normally this is done by modifying a couple of Mage_Adminhtml_Block_Customer_Grid grid block methods, _prepareCollection() and _prepareColumns(). In the case of this grid though I think that you can modify the _afterLoadCollection() method in order to reduce code duplication and increase version portability:

protected function _afterLoadCollection()
{
    if ($this->getCollection()) {

        //allows collection to be modified & reload from DB
        $this->getCollection()->clear(); 

        //re-load() collection
        $this->getCollection()->addAttributeToSelect('mobile')->load();
    }

    return parent::_afterLoadCollection();
}

Column logic overriding is easier:

protected function _prepareColumns()
{
    //execute original columns, which will execute sorting
    parent::_prepareColumns();

    //add your column with the ability to have final say in sort precedence
    $this->addColumn(
        'mobile',
        array( ... )
    )

    //re-run sorting
    $this->sortColumnsByOrder();

    //maintain fluent interface
    return $this;
}

Note that this is pseudocode which I have not tried. Also, note my comment regarding attribute code namespacing. "mobile" is ripe for collisions.

  • any way working fine. but I have question regarding this. actually this field value is save on customer_entity_varchar but this Column is Where will create? – Magento Nav Apr 17 '14 at 6:46
  • I don't understand what you mean by "but this Column is Where will create". To understand more about your attribute's behaviors, you need to find its record in the eav_attribute table. – benmarks Apr 17 '14 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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