Magento Registration Form : EZ Custom Fields
Hi Blog and All,
Over the past year I have become very in-tune with the zend framework platform. The few things that make working with zf is the ORM(Object Relational Mapping). In magento things get even more conflicting with the Advanced ORM aka Entity Attribute Value.
There are many ways to skin the cat, and magento/zend apps are no exception.
I have been slowing trudging through the Custom Module Route to adding fields to your registration form, and also biding them with backend & Db table. The most important thing to remember in Magento is that the Core codePool can be used along with any local module that you would want to add. Your going to extend the base classes, using your new Namespace/Module.
First Create A new XML file in etc/modules/Namespace_Module.xml
< ?xml version="1.0" ?>
true
local
The example shows Namespace->Music, Module->Customer
Second Create Copy The Existing function getDefaultEntities() from app/code/core/Mage/Customer/Entity/Setup.php
Add an array that specifies Your new Attribute at the very end of the first block like so
'artist_name' => array(
'label' => 'Artist Name',
'required' => false,
),
Third: You need to add the XML node, and the customize the module for the attribute name in file app/code/core/Mage/Customer/etc/config.xml
->Change From Mage_Customer
1.0.0.0
And Add the Attribute code to Create, Update.. Of CRUD(Create Read Update Delete). Settings of 1 equals Yes, for Required, and many other paired options.
Like so->
1
1
Fifth Step:
Once this is done, You just have to tell Magento to use the attribute in forms. This is the fun part.
You are going to want to create the php code below in your design/frontend/default/template/persistent/customer/register.phtml File.
/**
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'artist_name', array(
'label' => 'Artist Name',
'type' => 'varchar',
'input' => 'text',
'visible' => true,
'required' => false,
'position' => 1,
));
$usedInForms = array('customer_account_create', 'customer_account_edit', 'checkout_register', 'adminhtml_customer');
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'record_label');
$oAttribute->setData('used_in_forms', $usedInForms);
$oAttribute->save(); */
The best way to turn your new attribute into a registration field is to post the code in a comment. This is a snippet you will want to keep, because it is used to add your field to the table along with the forms you specify.
Change the value specifying record_label from the array, and the $oAttribute Object.
You want to upload all the previous files I have mentioned at this point, along with the register.phtml
The reason you would want to add this bit of code to this file per-say: All you have to do is visit url:site.com/customer/account/create. This will do the trick, but we have 2 more very quick alterations to make still..
We have to now add the fields to our edit.phtml, and register.phtml files. REMEMBER in Magento 1.6.1 you need to use the register.phtml file from the persistent template folder from within Customer. The edit.phtml file best used from /design/frontend/default/template/customer/forms/edit.phtml
< div class="input-box">< label for="artist_name">< ? php echo $this->__(' Artist Name') ?> < span class="required">* < input id="artist_name" class="required-entry input-text" title="< ?php echo $this->__('Artist Name') ? >" type="text" name="artist_name" value="< ?php echo $this- />htmlEscape($ this->getCustomer()-> getArtistName()) ? >" />
Keep in mind that you want to use the above code with the spaces removed, and notice the php code (getCustomer->getArtistName())
You will want to use the getCustomer method with edit.phtml.
For the register.phtml make sure to switch the getCustomer-> with getFormData->
Once you have all of these changes completed, you should have the attribute available, and functioning.
TO see the result, visit
Thanks for the read,
Rob
http://musicnation.net/customer/account/create/

