Friday, September 6, 2013

Advanced Newsletter Magento, How to add Custom field in Magento newsletter module,



Magento newsletter additional fields, Add fields in newsletter in magento, How to add new field(suscriber_name) in newsletter, How to add Custom field in Magento newsletter module, Advanced Newsletter Magento.

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,
1app\design\frontend\{YOUR_THEME}\default\template\newsletter\subscribe.phtml
Then add the following lines of code in your themes subscribe.phtml,
1<div class="input-box">
2  <?php echo Mage:getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()); ?>
3</div>
SUBSCRIBe_phtml
Now we’ve to get the country Id from the frontend select box using form POST method in controller. To do that we’ve to modify magento core files which is in
1app\code\core\Mage\Newsletter\controllers\SubscribeController.php
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:
1$email = (string) $this->getRequest()->getPost('email');
and enter this code below :
1$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:
1$status = Mage::getModel('newsletter/subscriber')->subscribe($email);
Just add the $country variable as a parameter to the function in that subscription function like this:
1$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:
controller
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
1Mage::getModel('newsletter/subscriber')
that  Subscriber.php file will be inside Newsletter folder within model folder like this:
1app\code\core\Mage\Newsletter\Model\subscriber.php
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.
1$this->setCountry($country);
 This will set the country id in country field in “newsletter_subscriber” table. Check out the image below if you’ve any doubts in subscriber.php:subscribe_php
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:
1app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber
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(…)) :
1$this->addColumn('Country', array(
2            'header'    => Mage::helper('newsletter')->__('Country'),
3            'index'     => 'country',
4            'type'      => 'country'
5 ));
grid-table
That’s it! I hope everything works for you. Let me know if you have any doubts through comments.

By PHP with No comments

0 comments:

Post a Comment

    • Popular
    • Categories
    • Archives