Magento – Adding custom fields on to frontend forms

databaseformssetup-script

I am trying to learn how to add fields onto a form and store them in the database. I am using the registration form for this exercise.

So far I have:

  • displayed an additional field
  • located relevant table in the db that stores the label (eav_attribute), names (customer_entity_varchar) and email (customer_entity)
  • set is_required to 1
  • displayed label next to field

My problem is that when I input data that field is not saved to the database, also I can leave it blank and the form will still be accepted. I assume this is because I added the data directly to my database (therefore bypassing other functions Magento needs to run).

I have looked around for an answer and found this, which seems to confirm my thought about needing to use a setup script for this task and I also came across this. Between the two of them I was pretty confident I could overcome this issue without any further help but I can't work out:

from link 1

  • how customer_account_edit and customer_account_create are identified as being needed?

from link 2

  • what do the first and second parameters do in $this->addAttribute() in the setup script, I can see the third parameter is the values for each column?

  • how is the first parameter of setData() decided?

*snippet from link 2*

<address_setup>
    <setup>
        <module>Excellence_Address</module>
        <class>Mage_Customer_Model_Entity_Setup</class> <!-- This is the important thing-->
    </setup>
    <connection>
        <use>core_setup</use>
    </connection>
</address_setup>

Setup

$installer = $this;

$installer->startSetup();

$this->addAttribute('customer_address', 'govt_id', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Govt ID No#',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'govt_id')
    ->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
    ->save();
$installer->endSetup();

Best Answer

I managed to add a field to the registration page and have it saved to the database. thanks to the help of this tutorial. I have added my code which has done the job.

Sorry there is no explanation to the code or an answer to what is customer_account_edit and customer_account_create and how is setData() parameter decided. But I thought this code may be helpful for anyone else adding new fields. Hopefully someone with more knowledge will expand on that part

config.xml

<global>
    <fieldsets>
        <customer_account>
            <username>
                <create>1</create>
                <update>1</update>
                <name>1</name>
            </username>
        </customer_account>
    </fieldsets>

    <resources>
        <customforms_reginfo_setup>
            <setup>
                <module>Customforms_Reginfo</module>
                <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </customforms_reginfo_setup>
    </resources>
</global>

setup-0.1.0.php

 $installer = $this;

 $installer->startSetup();

 $this->addAttribute('customer', 'username', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Username',
'global' => 1,
'visible' => 1,
'required' => 1,
'user_defined' => 1,
'visible_on_front' => 1
 ));

Mage::getSingleton('eav/config')
->getAttribute('customer', 'username')
->setData('used_in_forms', array('adminhtml_customer', 'customer_account_edit','customer_account_create'))
->save();
$installer->endSetup();