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_editandcustomer_account_createare 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();
2 Answers 2
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();
-
feel free to mark it as accepted if it is a complete answer.David Manners– David Manners2014年05月21日 07:35:30 +00:00Commented May 21, 2014 at 7:35
There is another module by Excellence that you can take a look at over here:
http://excellencemagentoblog.com/magento-advanced-shipping-method-development
This basically implements ONE new field to checkout which is stored in the DB and has custom JavaScript to make it compulsory.
You can use this website
http://freegento.com/ddl-magento-extension.php
to get the source code downloadable as a package.
Let me know if there's something you're unclear about.