Sunday, 24 February 2013

Add fields in newsletter in magento

1- Create app/code/local/[package-name]/[module-name]/sql/mysql4-install-0.1.0.php and now the following code:-


$installer = $this;

$installer->startSetup();

$installer->run("
 ALTER TABLE {$installer->getTable('newsletter_subscriber')}
 ADD (`lastname` TEXT NULL,
 `firstname` TEXT NULL)
 ");

$installer->endSetup();

2- Create app/code/local/[package-name]/[module-name]/controllers/[controller-name].php(like:- IndexController.php)

Code to add first name and last name -



require_once 'Mage/Newsletter/controllers/SubscriberController.php';
class Packagename_Modulename_IndexController extends Mage_Newsletter_SubscriberController
{
    /**
      * New subscription action
      */
    public function newAction()
    {.
     .
     .
     .
                 $status = Mage::getModel('newsletter/subscriber')->subscribe($email);

/*custom fields*/
if ($this->getRequest()->getPost('firstname') || $this->getRequest()->getPost('lastname'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $firstname     = (string) $this->getRequest()->getPost('firstname');
    $lastname     = (string) $this->getRequest()->getPost('lastname');
                     $subscriber->setFirstname($firstname)->setLastname($lastname);
                     $subscriber->save();
                }
/*custom fields*/
         .
         .
    }


3- Code to see the information on Admin-

    Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php

    Add this code :

    $this->addColumn('subscriber_firstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        ));

and you are done...

1 comment:

  1. Hi,
    I have followed all steps but when I submitted I couldn't see the name in magento admin and when i checked the newsletter subscriber table I can't find the field there also.

    Thanks for the post

    ReplyDelete