How to create custom fields, such as a mobile phone number in the customer registration form, without modifying the magento core files?

How to create custom fields, such as a mobile phone number, in a customer registration form without modifying magento core files? I tried, but the mobile number is not saved in the database

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 adding the following code to the array in getDefaultEntities

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

also my configuration file contains the following code

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

I also have this field in the 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 something I missed in this configuration? I want to save the mobile number in the database and then get it.

+4
source share
2 answers

I had to create a similar field (in this case, for this) for the corporate client, so here is how it turned out:

First, I created a module that would have all this fun. I added my own object installation record for the client, so I would not have to rely on the default client or rewrite any models unnecessarily (over time, you begin to conflict with yourself when overwriting). Your entity code seems to work, so it is up to you if you want to reorganize it.

Then I added a field to the client fields, which seem to help Magento understand what data needs to be stored 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> 

Then I added the field to the client form, just as you did above. During the verification process, I was forced to add a redefining model to one page in order to save data during the verification (in my case, only during invoicing):

 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. Hooray!

Hope this helps! Thanks Joe

+1
source

As for your code:

  • You do not need to override Mage_Customer_Model_Entity_Setup because this class is only used to set client attributes. The solution is to have your own customization class that inherits from Mage_Customer_Model_Entity_Setup.

  • You also need to install mysql4-install file

To learn how to add new attributes for a client, see this magento module I made: https://code.google.com/p/magento-code-snippets/source/browse/#svn/trunk/PWS_ExtCustomerFields

0
source

All Articles