Some times you want to add custom field in newsletter module in magento. I hope this may be a basic brick for you to start. For now I’m going to explain here on adding country field to newsletter module in magento. You can follow this tutorial to add any custom fields in your newsletter module.
First step is to add new field called ‘country’ in “newsletter_subscriber” table inside your magento database. We’re going to save only country id’s in this field. So datatype can be VARCHAR(5 or 3). Because I don’t know how length is the country id. Mostly I’ve seen only two characters, but in case you know.
Now open the file,
Then add the following lines of code in your themes subscribe.phtml,
<div class="input-box"> <?php echo Mage:getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()); ?> </div>
So as our ancestors says, please copy that file and place it in local folder for your store’s safety. I hope you know about these stuffs. Then open SubscribeController.php which is in local and edit the file to get the POST values from the form. Check out the function newAction() in that file. There’ll be a line declared with variable $email like given below:
$email = (string) $this->getRequest()->getPost('email');
and enter this code below :
$country = $this->getRequest()->getPost('country_id');
This is used to get the POST values from the front-end form in variable $country. Now we’ve to store it in database. So now find this line in the same function and modify it as follows:
$status = Mage::getModel('newsletter/subscriber')->subscribe($email);
Just add the $country variable as a parameter to the function in that subscription function like this:
$status = Mage::getModel('newsletter/subscriber')->subscribe($email,$country);
Check out the image below, if you don’t have any idea of what I’m saying:
Now we’re gonna find out where that subscribe function is and then we’ll add the $country parameter to it. Yeah! now I can feel your frustation. Only 2 more steps baby, we’ll reach the destination. As you can see from this code
that Subscriber.php file will be inside Newsletter folder within model folder like this:
make a copy into local folder and edit the subscribe() function in that file. Just add the $country variable as a parameter and add this line before the end of the function.
Now the form will be stored in db field country. If you dont believe check it in your DB dude! And finally one more step is there to show it in backend. Now open Grid.php from the following location:
and copy it in local folder and edit the files to display country field in backend grid table. There’ll be a function called _prepareColumns() in that Grid.php file. Add these lines below $this->addColumn(’email’, array(…)) :
$this->addColumn('Country', array( 'header' => Mage::helper('newsletter')->__('Country'), 'index' => 'country', 'type' => 'country' ));
That’s it! I hope everything works for you. Let me know if you have any doubts through comments.