0
votes

I'm trying to add a dropdown for selecting customer group on Magento onepage checkout, after that I need to save that information on customer profile.

My billing 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());
        }
    }

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

Can someone explain me how to pass the dropdown value to the observer? It's all day that I'm trying to make this work :(

1

1 Answers

2
votes

Only one mistake, your form field name for group id is billing['group_id']. Try below code

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()); 
      }