views:

163

answers:

2

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.

+1  A: 

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) {
        // 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

Joseph Mastey
@Joseph, wouldn't it be better to bind an Observer to the checkout billing save event and add the data into the session at that point? I'd be very cautious about rewriting anything to do with `Mage_Checkout` or `Mage_Sales`...
Jonathan Day
@Jonathan I know the feeling, but it has actually worked swimmingly. There was other code going on at the time (so I needed access to other methods in that class anyway), but I think your approach would also be workable.
Joseph Mastey
fair enough, sounds like the approach suited the requirements!
Jonathan Day
A: 

Regarding your code:

  1. You don't have to override the Mage_Customer_Model_Entity_Setup because this class is only used to install the customer attributes. The solution is to have your own setup class that inherits from the Mage_Customer_Model_Entity_Setup.

  2. You also need a mysql4-install setup file

To see exactly how to add new attributes to customer see this magento module I've made: https://code.google.com/p/magento-code-snippets/source/browse/#svn/trunk/PWS_ExtCustomerFields

Anda B
Interestingly, his approach makes the mysql4-installblahblah file unnecessary, since Varien is taking care of creating one every time they upgrade the customer model. I agree that he should *not* be overriding the core setup file, though, which does make it necessary to add that file.
Joseph Mastey
So you're saying his code works without a mysql install file, he only has to wait a month or two for a magento upgrade.
Anda B
lol, not exactly, though a good point.
Joseph Mastey