When someone subscribes to the Magento newsletter I also want them to fill in their first and last name. I have added the two input fields to the form. The field names are 'firstname' and 'lastname'.
I'm going to extend Mage/Newsletter/controllers/SubscriberController.php
In newAction():
I have retrieved the post variables here:
$email = (string) $this->getRequest()->getPost('email');
$firstname = (string) $this->getRequest()->getPost('firstname');
$lastname = (string) $this->getRequest()->getPost('lastname');
I have added the following code after the success message:
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subscriber->setEmail("[email protected]");
$subscriber->setFirstname("ADAM");
$subscriber->setLastname("MOSS");
$subscriber->save();
I just want to get the first and last name method working - the setEmail part works perfectly. I looked in Model/Subscriber.php and saw that there's no function for setting subscriber names.
Looking at the grid in the admin I also noticed that it says 'Customer First Name' and 'Customer Last Name' so I assume the names belong to the customer model rather than the subscriber model.
Is there a way around this? I want to be able to assign the customer name to the subscriber when they subscribe to the newsletter. I tried the observer method mentioned on this post, but I think the same issue exists: Adding a custom field to Magento's subscription module
Any help would be much appreciated.
You must create a local copy of: app/code/core/Mage/Newsletter/controllers/SubscriberController.php app/code/core/Mage/Newsletter/Model/Subscriber.php
In app/code/core/Mage/Newsletter/controllers/SubscriberController.php add after line 44:
Then change from:
to:
Now in app/code/core/Mage/Newsletter/Model/Subscriber.php edit the subscribe function signature at line 307 from:
to:
Add this after line 338:
And add the getters and setters for it:
You also need to add firstname and lastname columns to newsletter_subscriber as said in the other anwser.