Monday, August 20, 2012
Add first/last name in newsletter module Magento
>> First add two fields ( subscriber_firstname, subscriber_lastname) in newsletter_subscriber table and app\design\frontend\THEME\default\template\newsletter\subscribe.phtml.
>> Now edit this page : app\code\core\Mage\Newsletter\controllers\SubscriberController.php
Here you'll find "public function newAction()"
Paste the below code after this following code:
$status = Mage::getModel('newsletter/subscriber')->subscribe($email); //line (63)
///////////////////////// First Name //////////////////////////////
if ($this->getRequest()->getPost('subscriber_firstname'))
{
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$name = (string) $this->getRequest()->getPost('subscriber_firstname');
$subscriber->setSubscriberFirstname($name);
$subscriber->save();
}
////////////////////// END ///////////////////////////////////////
Do same for field Last Name....
>> The above code will effect your frontend. If you want to see the information from Admin too, you have to implement a little bit.
Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php
Add this code :
//It will show the information to the admin on News Letter Subscriber Section.
$this->addColumn('subscriber_firstname', array(
'header' => Mage::helper('newsletter')->__('Subscriber First Name'),
'index' => 'subscriber_firstname',
'default' => '----'
));
*Note :- I would suggest to override the subscriber controller and admin blocks. In this post I've focused in the logic only and shown in 3 steps to understand you its very simple. But it's not good programming. You should follow the magento module structure. In my another post you'll see that how to override frontend and admin controllers.
*********** Good Luck **********
Subscribe to:
Post Comments (Atom)
Correct me if I'm wrong, but shouldn't it be $sql="UPDATE ".$productWriteTable . " SET subscriber_firstname='".$name."' WHERE subscriber_id='$id"';
ReplyDeleteIt may also be worth noting that the code added to SubscriberController should go at the end of the function newAction() to be able to get the id
DeleteThanks for your reply.... Actually it was my old code. I've updated it, see the post..
DeleteBut still I couldn't understand your question...
I appreciate your attempts to teach people something, but you should really look into magento's observers and overriding blocks etc. Encouraging people to edit core files is bad practice.
ReplyDeleteHi! Alan,
DeleteThanks for your comments. :) I've added a note below the post.
Has anyone got this to work? I tried this but it didn't work for me.
ReplyDeleteYes it works for me. Every post in this blog was posted after my implementation. Actually it's my diary in which I've written all my work that I did. :)
DeleteSee that you've override your newsletter module properly. OR have you changed the core file directly?
There are three steps:
1) Add the fields in the table.
2) Add the form field in the phtml page.
3) Change the respective controller action.
that's enough for work. I've also done with the admin view.
it's not working my magento site version 1.6.1.0
ReplyDelete