Magento set subscriber first & last name

8.1k Views Asked by At

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.

2

There are 2 best solutions below

0
On

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:

$firtname = (string) $this->getRequest()->getPost('firstname');
$lastname = (string) $this->getRequest()->getPost('lastname');

Then change from:

$status = Mage::getModel('newsletter/subscriber')->subscribe($email);

to:

$status = Mage::getModel('newsletter/subscriber')->subscribe($email,$firtname,$lastname);

Now in app/code/core/Mage/Newsletter/Model/Subscriber.php edit the subscribe function signature at line 307 from:

public function subscribe($email)

to:

public function subscribe($email,$firstname,$lastname)

Add this after line 338:

$this->setSubscriberFirstname($firstname);
$this->setSubscriberLastname($lastname);

And add the getters and setters for it:

/**
* Alias for getSubscriberFirstname()
*
* @return string
*/
public function getFirstname()
{
    return $this->getSubscriberFirstname();
}

/**
* Alias for setSubscriberFirstName()
*
* @param string $value
*/
public function setFirstname($value)
{
    return $this->setSubscriberFirstname($value);
}

/**
* Alias for getSubscriberLastname()
*
* @return string
*/
public function getLastname()
{
    return $this->getSubscriberLastname();
}

/**
* Alias for setSubscriberLastname()
*
* @param string $value
*/
public function setLastname($value)
{
    return $this->setSubscriberLastname($value);
}

You also need to add firstname and lastname columns to newsletter_subscriber as said in the other anwser.

0
On

Take a look at newsletter_subscriber table in the database. There is no firstname or lastname. You set these fields only on the newsletter model.

The subscribe() method from subscriber model associate the subscriber to customer id so the grid shows indeed the customer firstname and lastname.

If you want to have these fields also for subscribers that are not associated to cutomers you should modify the newsletter_subscriber database table by adding these two fields.