Monday, February 20, 2012

Magento: how to add new fields to customer account

First create xml file in etc/module named as: Bd_Customercustomfield
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customercustomfield>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Bd_Customercustomfield>
  </modules>
</config>

Then create config file of your module with following code
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customercustomfield>
      <version>0.1.0</version>
    </Bd_Customercustomfield>
  </modules>
  <global>
    <helpers>
      <customercustomfield>
        <class>Bd_Customercustomfield_Helper</class>
      </customercustomfield>
    </helpers>
<models>
 <customercustomfield>
<class>Bd_Customercustomfield_Model</class>
<resourceModel>customercustomfield_mysql4</resourceModel>
 </customercustomfield>
</models>
<resources>
 <customerattribute_setup>
<setup>
 <module>Bd_Customercustomfield</module>
 <class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
 <use>core_setup</use>
</connection>
 </customerattribute_setup>
 <customerattribute_write>
<connection>
 <use>core_write</use>
</connection>
 </customerattribute_write>
 <customerattribute_read>
<connection>
 <use>core_read</use>
</connection>
 </customerattribute_read>
</resources>
  </global>
</config>

At last create a mysql file mysql4-install-0.1.0.php with following code
<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "customer_mobile",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Mobile Number",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Customer Mobile Number"

));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customer_mobile");

       
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
        $attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
        $attribute->save();
$installer->endSetup();
?>


9 comments:

  1. Hi, nice tutorial..

    I have one doubt. What´s the folder of code for the source model??

    <?php
    class Excellence_Profile_Model_Entity_School extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
    {
    public function getAllOptions()
    ......
    }
    }

    I´m using Magento 1.6.2

    Thanks

    ReplyDelete
  2. Hey, create directory as
    app\code\local\Excellence\Profile\Model\Entity\School.php

    ReplyDelete
  3. Open following file
    shop/app/design/frontend/default/helloeuropa/template/customer/form/register.phtml

    And copy/paste setShowAddressFields(true)?> at the top

    ReplyDelete
  4. thank you for the post. really useful one. i spent much time for learning some information about magento coding. but i make a conclusion that use magento extensions more profitable than try to do some things by yourself.

    Not some time ago i bought amasty's customer attributes extension and now i don't know any problems with attributes

    ReplyDelete
  5. Hi Bhoopendra ,

    cool blog,Thanks for sharing this information .



    Magento Developers

    ReplyDelete
  6. Dear Bhupendra,

    I don't understand why any body do not write the exact things for each & every step...To get the options you have written to put getalloptions() function but the thing is I do not have any custom module then where to put that code? I searched approx. all the articles but none said where to put this code in case if you do not have custom module. Can you please give solution for this?

    Thanks
    Kaushal

    ReplyDelete
  7. Ctrl+C Ctrl+V detected
    http://www.excellencemagentoblog.com/customer-registration-fields-magento1-6

    ReplyDelete
  8. Hello, I need some help in the magento theme creation. If possible can you share your email id with me or if you dont mind can you share your gtalk r skype or fb id. So that i can discuss with you.

    Thanks

    ReplyDelete
    Replies
    1. It's ok. You can contact me on d.bhoopendra@gmail.com.

      Delete