如何为客户创建新字段 [英] How to create new fields for customer

查看:66
本文介绍了如何为客户创建新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用magento 1.6版开发一个网站.我尝试为客户注册创建新字段,但未创建.我按照与1.5版相同的方式进行操作.

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.

1.6版中创建客户字段的任何变化?

Any variation in create customer fields in 1.6?

推荐答案

我不知道您尝试了什么,所以我将列出将新的schooL客户属性添加到Magento 1.6.1所需的所有步骤.注册表.

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. 最好创建一个模块,或将类似的代码放置在某个.phtml文件中并运行一次.如果您正确执行此操作并创建了一个模块,则将这样的代码放入mysql_install文件中:

  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();
?>

  • 在模块的config.xml文件中.请注意,我的模块名称是Excellence_Profile.

  • 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>
    

  • 在这里,我们将把属性添加到客户注册表单中.在1.6.0(+)版本中,使用的phtml文件为persistance/customer/register.phtml;在1.6.0(-)版本中,使用的phtml文件为customer/form/register.phtml 因此,我们需要基于magento版本打开phtml文件,并将此代码添加到代码中.

  • 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>
    

  • 对于magento 1.4.2(+),这是注册步骤所需的全部.如果您从此处创建用户,则应该在admin中看到学校文本字段. 对于magento 1.4.1(-),我们需要做另一件事,打开您的模块config.xml文件并添加:

  • 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>
    

  • 一旦用户在MyAccount-> Account Information部分中创建了他的帐户,他也应该能够编辑学校字段.为此,打开phtml文件customer/form/edit.phtml并将代码放入:

  • 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>
    

  • 注册表格也显示在magento的结帐页面中.要在此处添加字段,您需要为magento版本1.6(-)编辑checkout/onepage/billing.phtml,为magento版本1.6(+)文件编辑persistant/checkout/onepage/billing.phtml,然后找到代码:

  • 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>
    

    接下来打开您的模块config.xml或任何其他config.xml文件,添加以下行:

    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>
    

  • 接下来,我们需要对报价表(即magento中的sales_flat_quote表)进行一些更改.如果您有模块,请创建sql文件的升级版本,然后输入以下代码:

  • 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
    ");
    

  • 执行此操作后,请确保清除magento缓存,尤其是"Flush Magento Cache"和"Flush Cache Storage". 现在,当您下订单时,将使用正确的学校属性创建客户.

    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.

    这篇关于如何为客户创建新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆