【问题标题】:How to create new fields for customer如何为客户创建新字段
【发布时间】:2012-01-15 14:37:22
【问题描述】:

我正在使用 magento ver-1.6 开发一个网站。我正在尝试为客户注册创建新字段,但没有创建。我遵循了我们在 1.5 版中遵循的相同方式。

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

【问题讨论】:

    标签: magento magento-1.5


    【解决方案1】:

    我不知道您尝试了什么,所以我将列出将新的学校客户属性添加到 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();
      ?>
      
    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>
      
    3. 在这里,我们将属性添加到客户注册表单中。在版本 1.6.0(+) 中使用的 phtml 文件是 persistance/customer/register.phtml,在版本 1.6.0(-) 中使用的 phtml 文件是 customer/form/register.phtml 所以我们需要打开phtml文件,根据magento版本,在标签中添加这段代码。

      <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. 对于 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>
      
    5. 一旦用户在 MyAccount->Account Information 部分创建了他的帐户,他也应该能够编辑学校字段。为此打开 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>
      
    6. 注册表单也会显示在 magento 的结帐页面上。要在此处添加您的字段,您需要编辑 checkout/onepage/billing.phtml 用于 magento 版本 1.6(-) 和 persistant/checkout/onepage/billing.phtml 用于 magento 版本 1.6(+) 文件,然后找到代码:

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

      在此 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>
      

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

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

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

    【讨论】:

    • 好东西谢谢!对于 'source'=> 'profile/entity_school' 我们需要为那个创建一个特定的模型,我不需要它,但对于其他人来说,举个例子会很酷。
    【解决方案2】:

    我无法在 checkout_register 表单中保存新字段。

    我不得不扩展 global->fieldsets 节点:

    <global>
        <fieldsets>
            <checkout_onepage_quote>
                <customer_school>
                    <to_customer>school</to_customer>
                </customer_school>
            </checkout_onepage_quote>
    
            <checkout_onepage_billing>
                <school>
                    <to_customer>*</to_customer>
                </school>
            </checkout_onepage_billing>
    
            <customer_account>
                <school>
                    <to_quote>customer_school</to_quote>
                </school>
            </customer_account>
    
            <sales_convert_order>
                <customer_school>
                    <to_quote>*</to_quote>
                </customer_school>
            </sales_convert_order>
         </fieldsets>
    </global>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多