Magento 1.8 – Fix Customer Custom Tab Data Not Saving in Admin

adminevent-observermagento-1.8

I have a custom module which is used to add a custom module in customer section in admin. I have added this tab successfully and now I am trying to save custom data that obtain through custom tab in a custom table. For that I am using an observer method and which is shown below

<config>
-----------------
<global>
    <events>
      <adminhtml_customer_prepare_save>
          <observers>
              <save_customnewtab_data>
                  <type>singleton</type>
                  <class>customernewtab/observer</class>
                  <method>saveCustomerNewTabData</method>
              </save_customnewtab_data>
          </observers>
      </adminhtml_customer_prepare_save>
    </events>   
    --------------
 </global>
 ---------
 </config>

and my observer is shown below

<?php

class Mysite_Customernewtab_Model_Observer
{
    public function saveCustomerNewTabData($observer){

        if (isset($observer['customer'])) 
        {
            $customer = $observer['customer']->getData();
            echo "<pre>";
            print_r($customer);
            die();

        } 
        else 
        {
            //Do something with the customer
        }
    }
}

this is the extract of phtml file which I am using to render the content of my custom tab

<!-- some contents here -->
<tr>
<td>
    <table class="form-list" cellspacing="0">
    <tbody>
        <tr>
                    <td class="value">
            <input id="customer_nick_name" class=" required-entry input-text required-entry" type="text" value="" name="customernewtab[customer_nick_name]" />
        </td>
        </tr>
    </tbody>
    </table>
</td>
</tr>
<?php /*profile pic section*/ ?>
 <tr>
<td>
    <table class="form-list" cellspacing="0">
    <tbody>
        <tr>
        <td>
            <div id="customernewtab-prof-pic" style=" margin-left: 250px;margin-top: 30px;">                
                <input id="customernewtab_file_name" type="hidden" name="customernewtab[customer_photo]" value=""  />
            <input id="customnewtab_file" type="file" name="customer_prof_pic" value="" />

            </div>
        </td>
        </tr>
             </tbody>
         </table>
     </td>
 </tr>

Now see my observer output, while editing an existing customer data with my custom data has set along with it.

Array
(
[entity_id] => 3
[entity_type_id] => 1
[attribute_set_id] => 0
[website_id] => 1
[email] => rajeev_ktomy@yahoo.in
[group_id] => 1
[increment_id] => 
[store_id] => 1
[created_at] => 2014-03-10T04:23:00-07:00
[updated_at] => 2014-03-12 07:22:00
[is_active] => 1
[disable_auto_group_change] => 0
[firstname] => test
[lastname] => test
[password_hash] => 4de63e833f14934a3c5fd4213496a13c:n0seA0z1a135nmY3rtbTkUAmkTpiomzw
[created_in] => English
[prefix] => 
[middlename] => 
[suffix] => 
[taxvat] => 
[dob] => 
[gender] => 
[is_subscribed] => 
)

As you can see my custom data is missing in the output. My question is

1) Why my custom datas customer_nick_name and customer_photo are not visible in the output ?
2)What is wrong in my code ?
3) Is this the correct way to save custom tab data ?

Best Answer

Why my custom datas customer_nick_name and customer_photo are not visible in the output ?

Because they are not processed, this happens here:

\Mage_Adminhtml_CustomerController::saveAction
/** @var $customerForm Mage_Customer_Model_Form */
$customerForm = Mage::getModel('customer/form');
$customerForm->setEntity($customer)
    ->setFormCode('adminhtml_customer')
    ->ignoreInvisible(false)
;

$formData = $customerForm->extractData($this->getRequest(), 'account');

You need to add the attributes which should be parsed to the customer_form_attribute table, if you don't use the magento logic.

What is wrong in my code ? Is this the correct way to save custom tab data ?

But why aren't you just add a new attribute and let magento do the rest? Magento should render the attributes and show them to you ... and save them of course.

There are lots of blog articles how to add a customer attribute, e.g. this one from elsondesigns.

$this->addAttribute($entity, 'some_attribute', array(
    'type' => 'text',               /* input type */
    'label' => 'Some Attribute',    /* Label for the user to read */
    'input' => 'text',              /* input type */
    'visible' => TRUE,              /* users can see it */
    'required' => FALSE,            /* is it required, self-explanatory */
    'default_value' => 'default',   /* default value */
    'adminhtml_only' => '1'         /* use in admin html only */
));

Adding an image field is possible, the part with the enctype is a little bit tricky (good catch!) and I think you need an observer for it... and I'm WRONG. Everything fine:

\Mage_Adminhtml_Block_Customer_Edit_Form
protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
        'id'        => 'edit_form',
        'action'    => $this->getData('action'),
        'method'    => 'post',
        'enctype'   => 'multipart/form-data'
    ));
Related Topic