Magento:为组创建自定义字段并将数据传递给观察者

时间:2015-07-17 13:09:26

标签: php magento

我正在尝试在Magento onepage checkout上添加一个用于选择客户群的下拉列表,之后我需要将这些信息保存在客户档案中。

我的结算PHTML:

 <div class=”select”
    <label for=”billing:group_id”><?php echo $this->__('Select Group') ?> <span class=”required”>*</span></label><br />
    <select id=”billing:group_id” name=”billing[group_id]” title=”<?php echo $this->__('Customer Group') ?>” class=”validate-select”>
        <option value=””><?php echo $this->__('Select Group') ?></option>
        <?php
            $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionArray();
        foreach ($groups as $a)
        echo "<option value='".$a['value']."'>". $a['label']. "</option>";
        ?>
    </select>
</div>

My Observer.php

public function customerSaveBefore($observer)
    {
        try {
            $customer = $observer->getCustomer();
            $group= $this->getRequest()->getPost('group_id');
            $customer->setData('group_id', $group);
        } catch ( Exception $e ) {
            Mage::log("customer_save_before observer failed: " . $e->getMessage());
        }
    }

我的config.xml:

<customer_save_before>
    <observers>
        <qwe_customer_save_observer>
            <type>singleton</type>
            <class>Ntt_Customer_Model_Observer</class>
            <method>customerSaveBefore</method>
        </qwe_customer_save_observer>
    </observers>
</customer_save_before>

有人可以解释一下如何将下拉值传递给观察者吗?这一整天都是我努力做到这一点:(

1 个答案:

答案 0 :(得分:2)

只有一个错误,您的组ID的表单字段名称为billing['group_id']。请尝试下面的代码

function customerSaveBefore($observer) {
     try { 
       $customer = $observer->getCustomer(); 
       $post= Mage::app()->getRequest()->getPost('billing'); 
        $customer->setData('group_id', $post['group_id']); 
      } 
      catch ( Exception $e ){ 
          Mage::log("customer_save_before observer failed: " . $e->getMessage()); 
      }