在结帐中显示自定义客户字段 [英] Show Custom Customer Fields in Checkout

查看:48
本文介绍了在结帐中显示自定义客户字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地向客户添加了自定义字段.但是,我需要这些字段才能显示在一页结帐中.

I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.

我重写了Mage_Customer_Block_Widget_Name并创建了自己的customer/widget/name.phtml,在sql/xxx_setup/installer-x.y.z.php中添加了属性(将它们添加到了adminhtml_customercustomer_account_editcheckout_registercustomer_account_create中),并且它们可以正常工作在管理站点中,但是他们只是不会在结帐表单上工作.该字段显示为,但其值错误且没有标签.

I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.

我一无所知,为什么它不能在客户注册表格中使用,而不能在结帐中使用.

I'm clueless why does it work in the customer registration form but doesn't in the checkout.

添加属性的安装程序代码为:

The installer code to add the attribute is:

$attributes = array(
    'lastname2' =>  array(
        'frontend_label'=>'Apellido Materno',
        'label' => 'Apellido Materno',
        'input' => 'text',
        'type'  => 'varchar',
        //System =  False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
        'system'=>true,
        'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
        'user_defined'=>false,
        'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
        'required' => 0,
        'position' =>69
    ));


foreach($attributes as $attribute_code=>$definition)
    {
        $installer->addAttribute('customer', $attribute_code,  $definition); 

        /**
        * @var Mage_Eav_Model_Config
        */
        Mage::getSingleton('eav/config')
        ->getAttribute('customer', $attribute_code)
        ->setData('used_in_forms',$definition['used_in_forms'])
        ->save();
    }

name.phtml中的代码为

The code in name.phtml is

<div class="<?php echo $this->getContainerClassName()?>">
    <?php if ($this->showPrefix()): ?>
        <div class="field name-prefix">
            <label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
            <div class="input-box">
                <?php if ($this->getPrefixOptions() === false): ?>
                    <input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
                    <?php else: ?>
                    <select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
                        <?php foreach ($this->getPrefixOptions() as $_option): ?>
                            <option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
                        <?php endforeach; ?>
                </select>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    <div class="field name-firstname">
        <label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>
    <?php if ($this->showMiddlename()): ?>
        <?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
        <div class="field name-middlename">
            <label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
            <div class="input-box">
                <input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
            </div>
        </div>
        <?php endif; ?>
    <div class="field name-lastname">
        <label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>

    <div class="field name-lastname">
        <label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
        <div class="input-box">
            <input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
        </div>
    </div>

    <?php if ($this->showSuffix()): ?>
        <div class="field name-suffix">
            <label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
            <div class="input-box">
                <?php if ($this->getSuffixOptions() === false): ?>
                    <input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
                    <?php else: ?>
                    <select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
                        <?php foreach ($this->getSuffixOptions() as $_option): ?>
                            <option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
                        <?php endforeach; ?>
                </select>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
</div>

推荐答案

您正在谈论的结帐中的此字段是地址属性,而不是客户属性.因此,您需要以不同的方式加载它们.对于注册用户,您可以使用Mage::getSingleton('customer/session')->getCustomer()->getLastname2(),但由于尚无属性,它不会保存到您的地址.

This fields in the checkout you are talking about are address attributes, not customer attributes. So you need to load them differently. For registered users you could use Mage::getSingleton('customer/session')->getCustomer()->getLastname2() but it won't be saved to your address, as there is no attribute yet.

根据您希望lastname2可以访问的位置,可以为实体customer_addressquote_addressorder_address创建相应的属性.它们的创建方式与使用

Depending on where you want lastname2 to be accessible you can create corresponding attributes for the entities customer_address,quote_address and order_address. They are created the same way you did for customer with

$installer->addAttribute($entityName, $attribute_code,  $definition); 

但这还不是全部.为了正确转换属性,您需要在模块config.xml中设置转换规则.例如,请参见Mage_Sales的配置. 在global节点中,有一个带有相应规则的节点fieldsets.有一个节点customer_address将地址属性转换为引用地址属性.在sales_convert_quote中,有一些规则可以将此属性转换为订单属性.

But that's not all. For the attributes to be converted correctly you need to set up conversion rules in your module config.xml. See for example the configuration of Mage_Sales. In the global node there is a node fieldsets with the corresponding rules. There is a node customer_address to convert address attributes to quote address attributes. In sales_convert_quote there are rules to convert this attributes to order attributes.

因此,要使您的属性在所有配置中都可以访问,就应该像这样:

So for your attributes to be accessible in all you config should look like this:

<global>
   <fieldsets>
      <customer_address>
         <lastname2>
            <to_quote_address>*</to_quote_address>
         </lastname2>
      </customer_address>
      <sales_copy_order_billing_address>
         <lastname2>
            <to_order>*</to_order>
         </lastname2>
       <sales_copy_order_billing_address>
       <sales_copy_order_shipping_address>
         <lastname2>
            <to_order>*</to_order>
         </lastname2>
       </sales_copy_order_shipping_address>
       <sales_convert_quote_address>
           <lastname2>
              <to_order_address>*</to_order_address>
              <to_customer_address>*</to_customer_address>
           </lastname2>
       </sales_convert_quote_address>
       <sales_convert_order_address>
            <lastname2>
               <to_quote_address>*</to_quote_address>
            </lastnam2e>
       <sales_convert_order_address>
       <customer_address>
           <lastname2>
              <to_quote_address>*</to_quote_address>
           </lastname2>
       </customer_address>
   </fieldsets>
</global>

这篇关于在结帐中显示自定义客户字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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