How do I create custom fields like mobile number in customer registration form without modifying magento core files?

Sreejith Sasidharan picture Sreejith Sasidharan · Sep 29, 2010 · Viewed 8.7k times · Source

How do I create custom fields like mobile number in customer registration form without modifying magento core files? I tried but the mobile number is not saved in the DB

I override the Mage_Customer_Model_Entity_Setup class with Myown_Mage_Customer_Model_Entity_Setup by creating Model/Entity/Setup.php in my modules folder and added the following code to the array in getDefaultEntities

'mobilenumber' => array(
                        'label'        => 'Mobile Number',
                        'visible'    => true,
                        'required'    => true,
                    ),

also my config file contains following code

<models>
        <customer_entity>
            <rewrite>
                  <customer>Myown_Mage_Customer_Model_Entity_Setup</customer>
            </rewrite>
        </customer_entity>
    </models>

Also i have this field in template/customer/form/register.phtml

<input type="text" name="mobilenumber" id="mobilenumber" value="<?php echo $this->htmlEscape($this->getFormData()->getMobilenumber()) ?>" title="<?php echo $this->__('Mobile Number') ?>" class="required-entry input-text" />  

Is there any thing I miss in this configuration? I want to save the mobile number in the DB and retrieve it afterward.

Answer

Joseph Mastey picture Joseph Mastey · Oct 1, 2010

I had to create a similar field (referred_by in this case) for an Enterprise customer, so here's how that went:

First, I created a module that would house all this fun. I added my own entity setup entry for customer, so that I wouldn't have to rely on the default customer one or rewrite any models unnecessarily (as time goes on, you start to conflict with yourself on rewrites). Your entity code seems to work, so it's up to you if you want to refactor that.

Then I added the field to the customer fieldsets, which seems to help Magento understand what data to save in the database:

<global>
    <fieldsets>
        <customer_account>
            <referred_by>
                <create>1</create>
                <update>0</update>
                <to_order>customer_referred_by</to_order>
            </referred_by>
        </customer_account>
    </fieldsets> 
</global>

I then added the field to the customer form, similarly to the way you did above. In checkout, I was forced to add an overriding model to the onepage checkout to save the data during checkout (in my case, only during billing):

class Company_Module_Model_Checkout_Type_Onepage extends Mage_Checkout_Model_Type_Onepage {
    public function saveBilling($data, $customerAddressId) {
        if (isset($data['referred_by'])) {
            // set referred for later use.
            $session    = Mage::getSingleton("customer/session");
            $session->setData("referred_by", $data['referred_by']);
        }

        return parent::saveBilling($data, $customerAddressId);
    }//end saveBilling
}

And:

<global>
    <models>
        <checkout>
            <rewrite>
                <type_onepage>Company_Module_Model_Checkout_Type_Onepage</type_onepage>
            </rewrite>
        </checkout>
    </models>   
</global>

After that, the data was saved correctly in the database. Hurrah!

Hope that helps! Thanks, Joe