Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

magento - How to create new fields for customer

I am developing a website with magento ver-1.6. I am try to create new fields for customer registration, but it not created. I followed the same way what we followed in ver-1.5.

Any variation in create customer fields in 1.6?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know what you tried so I'm just going to list all the steps needed to add a new schooL customer attribute to the Magento 1.6.1 registration form.

  1. Create a module preferably, or place similiar code to this in some .phtml file and run it once. If you're doing this proper and creating a module, put code like this into the mysql_install file:

    <?php
    $installer = $this;
    $installer->startSetup();
    $setup = Mage::getModel('customer/entity_setup', 'core_setup');
    $setup->addAttribute('customer', 'school', array(
        'type' => 'int',
        'input' => 'select',
        'label' => 'School',
        'global' => 1,
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'default' => '0',
        'visible_on_front' => 1,
            'source'=> 'profile/entity_school',
    ));
    if (version_compare(Mage::getVersion(), '1.6.0', '<='))
    {
          $customer = Mage::getModel('customer/customer');
          $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
          $setup->addAttributeToSet('customer', $attrSetId, 'General', 'school');
    }
    if (version_compare(Mage::getVersion(), '1.4.2', '>='))
    {
        Mage::getSingleton('eav/config')
        ->getAttribute('customer', 'school')
        ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
        ->save();
    }
    $installer->endSetup();
    ?>
    
  2. In your module config.xml file. Note that the name of my module is Excellence_Profile.

    <profile_setup> <!-- Replace with your module name -->
     <setup>
      <module>Excellence_Profile</module> <!-- Replace with your module name -->
      <class>Mage_Customer_Model_Entity_Setup</class>
     </setup>
    </profile_setup>
    
  3. Here we will add our attribute, to the customer registration form. In version 1.6.0(+) the phtml file used is persistance/customer/register.phtml and in version 1.6.0(-) the phtml file used is customer/form/register.phtml So we need to open the phtml file, based on magento version and add this code in the tag.

    <li>
    <?php
    $attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
    ?>
    <label for="school" 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->getFormData()->getSchool() == $option['value']){ echo 'selected="selected"';}?>><?php echo $this->__($option['label'])?></option>
    <?php } ?>
    </select>
    </div>
    </li>
    
  4. For magento 1.4.2(+) that is all that is required for the registration step. If you create a user from here, you should see the school text field in admin. For magento 1.4.1(-), we need to do another thing open the your modules config.xml file and add:

    <global>
            <fieldsets>
                <customer_account>
                     <school><create>1</create><update>1</update><name>1</name></school>
                </customer_account>
            </fieldsets>
    </global>
    
  5. Once, user has created his account in the MyAccount->Account Information section he should be able to edit the school field as well. For this open the phtml file customer/form/edit.phtml and put in the code in the :

    <?php
    <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>
    
  6. A registration form also shows up at the checkout page in magento. To add you field here, you need to edit checkout/onepage/billing.phtml for magento version 1.6(-) and persistant/checkout/onepage/billing.phtml for magento version 1.6(+) file and then find the code:

    <?php if(!$this->isCustomerLoggedIn()): ?>
    

    inside this if condition add your field

    <li>
    <li>
    <?php
    $attribute = Mage::getModel('eav/config')->getAttribute('customer','school');
    ?>
    <label for="school" 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="billing[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 echo $this->__($option['label'])?></option>
    <?php } ?>
    </select>
    </div>
    </li>
    

    Next open your module config.xml or any other config.xml file, add the following lines:

        <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>
    
  7. Next we need to make some changes in the quote table i.e sales_flat_quote table in magento. If you have a module then create an upgrade version of your sql file and put in this code:

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

After doing this make sure to clear you magento cache, specifically “Flush Magento Cache” and “Flush Cache Storage”. Now when you place order, the customer is created with the correct school attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...