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
Because they are not processed, this happens here:
You need to add the attributes which should be parsed to the
customer_form_attribute
table, if you don't use the magento logic.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.
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: