如何为客户创建新字段

San*_*raj 7 magento magento-1.5

我正在使用magento ver-1.6开发一个网站.我尝试为客户注册创建新字段,但它没有创建.我按照与ver-1.5中所遵循的相同的方式进行操作.

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

geo*_*sey 22

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

  1. 最好创建一个模块,或者在一些.phtml文件中放置类似的代码并运行一次.如果您正确地执行此操作并创建模块,请将此类代码放入mysql_install文件中:

    <?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();
    ?>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的模块config.xml文件中.请注意,我的模块名称是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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在这里,我们将我们的属性添加到客户注册表单中.在版本1.6.0(+)中使用的phtml文件是persistance/customer/register.phtml在版本1.6.0( - )中使用的phtml文件是customer/form/register.phtml 因此我们需要打开基于magento版本的phtml文件并在标记中添加此代码.

    <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>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 对于magento 1.4.2(+),这是注册步骤所需的全部内容.如果您从此处创建用户,则应在admin中查看学校文本字段.对于magento 1.4.1( - ),我们需要做另一件事打开你的模块config.xml文件并添加:

    <global>
            <fieldsets>
                <customer_account>
                     <school><create>1</create><update>1</update><name>1</name></school>
                </customer_account>
            </fieldsets>
    </global>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 一旦用户在MyAccount->帐户信息部分创建了他的帐户,他也应该能够编辑学校字段.为此打开phtml文件customer/form/edit.phtml并输入以下代码:

    <?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>
    
    Run Code Online (Sandbox Code Playgroud)
  6. 注册表格也会显示在magento的结帐页面上.要在此处添加字段,您需要编辑checkout/onepage/billing.phtmlmagento版本1.6( - )和persistant/checkout/onepage/billing.phtmlmagento版本1.6(+)文件,然后找到代码:

    <?php if(!$this->isCustomerLoggedIn()): ?>
    
    Run Code Online (Sandbox Code Playgroud)

    在这个if条件中添加你的字段

    <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>
    
    Run Code Online (Sandbox Code Playgroud)

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

        <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>
    
    Run Code Online (Sandbox Code Playgroud)
  7. 接下来我们需要在报价表中进行一些更改,即magento中的sales_flat_quote表.如果您有一个模块,那么创建一个sql文件的升级版本并输入以下代码:

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

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