How to create an attribute field for customer account in Magento


Follow these steps to create attribute :
Step 1: Create this file and save on this location app\etc\modules\Excellence_Profile.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Excellence_Profile>
            <active>true</active>
            <codePool>local</codePool>
        </Excellence_Profile>
    </modules>
</config>

Step 2: Create this file and save on this location app\code\local\Excellence\Profile\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Excellence_Profile>
            <version>0.1.0</version>
        </Excellence_Profile>
    </modules>
    <frontend>
        <routers>
            <profile>
                <use>standard</use>
                <args>
                    <module>Excellence_Profile</module>
                    <frontName>profile</frontName>
                </args>
            </profile>
        </routers>
        <layout>
            <updates>
                <profile>
                    <file>profile.xml</file>
                </profile>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
            <profile>
                <use>admin</use>
                <args>
                    <module>Excellence_Profile</module>
                    <frontName>profile</frontName>
                </args>
            </profile>
        </routers>
    </admin>
    <global>
     <fieldsets>
       <checkout_onepage_quote>
            <customer_school>
             <to_customer>school</to_customer>
         </customer_school>
       </checkout_onepage_quote>
        <customer_account>
            <school>
                <to_quote>customer_school</to_quote>
            </school>
        </customer_account>      
      </fieldsets>
    </global>
    <global>
        <fieldsets>
            <customer_account>
                 <school><create>1</create><update>1</update><name>1</name></school>
            </customer_account>
        </fieldsets>
    </global>
    <global>
        <models>
            <profile>
                <class>Excellence_Profile_Model</class>
                <resourceModel>profile_mysql4</resourceModel>
            </profile>
            <profile_mysql4>
                <class>Excellence_Profile_Model_Mysql4</class>
                <entities>
                    <profile>
                        <table>profile</table>
                    </profile>
                </entities>
            </profile_mysql4>
        </models>
        <resources>
            <profile_setup>
                <setup>
                    <module>Excellence_Profile</module>
                    <class>Mage_Customer_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </profile_setup>
            <profile_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </profile_write>
            <profile_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </profile_read>
        </resources>
        <blocks>
            <profile>
                <class>Excellence_Profile_Block</class>
            </profile>
        </blocks>
        <helpers>
            <profile>
                <class>Excellence_Profile_Helper</class>
            </profile>
        </helpers>
    </global>
</config>


Step 3: Create folder & file and save on this location app\code\local\Excellence\Profile\controller\IndexController.php

<?php
class Excellence_Profile_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
       
        $this->loadLayout();    
        $this->renderLayout();
    }
}


Step 4: Create folder & file and save on this location app\code\local\Excellence\Profile\Helper\Data.php

<?php

class Excellence_Profile_Helper_Data extends Mage_Core_Helper_Abstract
{

}


Step 5: Create folder & file and save on this location app\code\local\Excellence\Profile\Block\Profile.php

<?php
class Excellence_Profile_Block_Profile extends Mage_Core_Block_Template
{
}


Step 6: Create folder & file and save on this location app\code\local\Excellence\Profile\Helper\Data.php

<?php
class Excellence_Profile_Model_Entity_School extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {

        if ($this->_options === null) {
            $this->_options = array();
            $this->_options[] = array(
                    'value' => '',
                    'label' => 'Select Option'
            );
            $this->_options[] = array(
                    'value' => 1,
                    'label' => 'Option1'
            );
            $this->_options[] = array(
                    'value' => 2,
                    'label' => 'Option1'
            );
            $this->_options[] = array(
                    'value' => 3,
                    'label' => 'Option3'
            );
           
        }

        return $this->_options;
    }
}

Step 7: Create folder & file and save on this location app\code\local\Excellence\Profile\sql\profile_setup\mysql4-intall-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'manufacturer', array(
    'type' => 'int',
    'input' => 'select',
    'label' => 'Manufacturer',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '0',
    'visible_on_front' => 1,
    'source' =>     'profile/entity_manufacturer',
));


if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
    $customer = Mage::getModel('customer/customer');
    $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
    $setup->addAttributeToSet('customer', $attrSetId, 'General', 'manufacturer');
}

if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
    Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'manufacturer')
    ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
    ->save();

}

$tablequote = $this->getTable('sales/quote');
$installer->run("
ALTER TABLE  $tablequote ADD  `customer_manufacturer` INT NOT NULL
");

$installer->endSetup();

Step 7: save this file in your frontend theme's layout folder

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
    </default>
    <customer_account_create>
        <reference name="customer_form_register">
            <action method="setTemplate"><template>profile/persistent/customer/form/register.phtml</template></action>
        </reference>
    </customer_account_create>
    <customer_account_edit>
        <reference name='customer_edit'>
            <action method="setTemplate"><template>profile/customer/form/edit.phtml</template></action>
        </reference>
    </customer_account_edit>
    <checkout_onepage_index>
        <reference name="checkout.onepage.billing">
            <action method="setTemplate"><template>profile/persistent/checkout/onepage/billing.phtml</template></action>
            </reference>
    </checkout_onepage_index>
</layout>


Step 8 :- To show attribute on front end add this code in your template file
 
<li>
                     <?php
                         $attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
                     ?>
                    <label for="is_active" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('School') ?></label>
                    <div class="input-box">
                        <select name="school" id="school" class="<?php if($attribute->getIsRequired() == true){?>required-entry<?php } ?>">
                            <?php
                                 $options = $attribute->getSource()->getAllOptions();
                                 foreach($options as $option){
                            ?>
                                <option value='<?php echo $option['value']?>' <?php if($this->getCustomer()->getSchool() == $option['value']){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
                            <?php } ?>
                        </select>
                    </div>
                </li>  
 

No comments:

Post a Comment