0

カスタム フィールドを顧客に追加することに成功しました。ただし、これらのフィールドを 1 ページのチェックアウトに表示する必要があります。

私はオーバーライドMage_Customer_Block_Widget_Nameして独自の を作成customer/widget/name.phtmlし、属性をsql/xxx_setup/installer-x.y.z.php追加しました ( adminhtml_customercustomer_account_editcheckout_registerおよびに追加しましたcustomer_account_create)。管理サイトでは正常に機能しますが、チェックアウト フォームでは機能しません。フィールドは表示されますが、値が正しくなく、ラベルがありません

顧客登録フォームでは機能するのに、チェックアウトでは機能しない理由がわかりません。

属性を追加するインストーラー コードは次のとおりです。

$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 のコードは

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

1 に答える 1

3

あなたが話しているチェックアウトのこのフィールドは、顧客属性ではなく住所属性です。したがって、それらを別々にロードする必要があります。登録ユーザーの場合は使用できますMage::getSingleton('customer/session')->getCustomer()->getLastname2()が、属性がまだないため、アドレスに保存されません。

lastname2 にアクセスできるようにする場所に応じて、エンティティcustomer_addressquote_addressおよびに対応する属性を作成できますorder_addresscustomerで行ったのと同じ方法で作成されます

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

しかし、それだけではありません。属性を正しく変換するには、モジュールの config.xml で変換規則を設定する必要があります。たとえば、 の構成を参照してくださいMage_Salesglobalノードにはfieldsets、対応するルールを持つノードがあります。customer_addressアドレス属性を引用アドレス属性に変換するノードがあります。このsales_convert_quote属性を注文属性に変換するルールがあります。

したがって、すべての構成で属性にアクセスできるようにするには、次のようにする必要があります。

<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>
于 2013-11-10T17:45:22.067 に答える