Wednesday, December 19, 2012

Add First name and last name in newsletters

  >> First add two fields ( subscriber_firstname, subscriber_lastname) in newsletter_subscriber table and app\design\frontend\THEME\default\template\newsletter\subscribe.phtml.

    >> Now edit this page : app\code\core\Mage\Newsletter\controllers\SubscriberController.php
        Here you'll find "public function newAction()"
            Add this Code:-
   
            if ($this->getRequest()->isPost() && $this->getRequest()->getPost('subscriber_firstname'))
            {
                 $email  = (string) $this->getRequest()->getPost('email');
                 $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                 $name     = (string) $this->getRequest()->getPost('subscriber_firstname');
                 $id  =  $subscriber->getId();
                 $write = Mage::getSingleton('core/resource')->getConnection('core_write');
                 $resource = Mage::getSingleton('core/resource');
                 //$table_prefix = Mage::getConfig()->getTablePrefix();   
                 $productWriteTable = $resource->getTableName('newsletter_subscriber');
               
                 $sql="UPDATE ".$productWriteTable . " SET subscriber_firstname='".$name."' WHERE subscriber_id=$id";
                 $write->query($sql);
            }
            Do same for field Last Name....


    >> The above code will effect your frontend. If you want to see the information from Admin too, you have to implement a little bit.

    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'   =>    '----'
        ));   

2 comments:

  1. Nice work man...
    it really helps me.

    ReplyDelete
  2. I'm having a problem where field change_status_at doesn’t populate when someone subscribes for the first time unless I change the structure of the table.

    Do you see any problem with changing the table structure with Magento of is there a fix to fill in the time when the customer signs up for a newsletter. Or know how to get it to update when user signs up?

    Field: change_status_at
    Type: TIMESTAMP
    Length/Values1:
    Default2: NULL

    Collation:
    Attributes:
    Null: \"CHECKED\"
    AUTO_INCREMENT:
    Comments: Change Status At

    macleon

    ReplyDelete