Magento sets the caller first and last name

When someone signs up for the Magento Newsletter, I also want them to fill in their first and last name. I added two input fields to the form. Field names: "firstname" and "lastname".

I am going to expand Mage / Newsletter / Controllers / SubscriberController.php

In newAction () :

I got post variables here:

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

After reporting success, I added the following code:

 $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email); $subscriber->setEmail(" example@email.com "); $subscriber->setFirstname("ADAM"); $subscriber->setLastname("MOSS"); $subscriber->save(); 

I just want the first and last name method to work - the setEmail part works fine. I looked at Model / Subscriber.php and saw that there is no function to set the names of subscribers.

Looking at the administrator’s grid, I also noticed that he says “Client name” and “Client surname”, so I assume that the names belong to the client model, not the subscriber model.

Is there any way around this? I want to be able to assign a customer name to a subscriber when they subscribe to the newsletter. I tried the observer method mentioned in this post, but I think the same problem exists: Adding a custom field to the Magento subscription module

Any help would be greatly appreciated.

+4
source share
2 answers

Take a look at the newsletter_subscriber table in the database. No name or name. You set these fields only on the newsletter model.

The subscribe () method from the subscriber model associates the subscriber with the client ID, so the grid really shows the name and surname of the client.

If you want to have these fields also for subscribers who are not related to cutomers, you must modify the newsletter_subscriber database table by adding these two fields.

+2
source

You must create a local copy: application / code / kernel / Mage / Newsletter / controllers / SubscriberController.php Application / code / kernel / Mage / Newsletter / model / Subscriber.php

In the /code/core/Mage/Newsletter/controller/SubscriberController.php application, 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); 

in

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

Now in app / code / core / Mage / Newsletter / Model / Subscriber.php edit the signature of the signature on line 307 with

 public function subscribe($email) 

in

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

Add this after line 338:

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

And add 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 the firstname and lastname columns to the newsletter_subscriber, as indicated by another anwser.

0
source

Source: https://habr.com/ru/post/1392903/


All Articles