Thursday 21 February 2013

Add extra Field to Newsletter

Step 1: First made the field in the newsletter_subscriber table. lets we made the field "country", varchar(100).

Step 2:  Go to there: app/code/core/Mage/Newsletter/Model/Mysql4/Subscriber.php

and add the following lines in the file:

protected function _prepareSave(Mage_Newsletter_Model_Subscriber $subscriber)
{
    $data = array();
    $data['customer_id'] = $subscriber->getCustomerId();
    $data['store_id'] = $subscriber->getStoreId()?$subscriber->getStoreId():0;
    $data['subscriber_status'] = $subscriber->getStatus();
    $data['subscriber_email']  = $subscriber->getEmail();
    $data['subscriber_confirm_code'] = $subscriber->getCode();

    //ADD A NEW FIELD START

    //note that the string index for the $data array
    //must match the name of the column created in step 1
    $data['country'] = $subscriber->getCountry();

   
}


Step 3: Then for field shown in the admin panel go to there: 
app/code/core/Mage/Adminhtml/Block/Newsletter/Subscriber/Grid.php

and add the following lines in this method  _prepareColumns().

$this->addColumn('country', array(
    'header'    => Mage::helper('newsletter')->__('Name'),
    //the index must match the name of the column created in step 1
    'index'     => 'country',
    'default'   =>    '----'
));
 
Step 4: then Go to app/code/core/Mage/Newsletter/controllers/SubscriberController.php to add
value in database.
 
public function newAction()
{
    if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
        $session   = Mage::getSingleton('core/session');
        $email     = (string) $this->getRequest()->getPost('email');

        try {
            if (!Zend_Validate::is($email, 'EmailAddress')) {
                Mage::throwException($this->__('Please enter a valid email address'));
            }

            $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
            if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                $session->addSuccess($this->__('Confirmation request has been sent'));
            }
            else {
                $session->addSuccess($this->__('Thank you for your subscription'));
            }

                //ADD COUNTRY INFO START

                //at this point we may safly assume that subscription record was created
                //let's retrieve this record and add the additional data to it
                $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);

                //assuming that the input's id is "country"
                $subscriber->setCountry((string) $this->getRequest()->getPost('country'));

                //don't forget to save the subscriber!
                $subscriber->save();

                //ADD COUNTRY INFO END
        }
        catch (Mage_Core_Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
        }
        catch (Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription'));
        }
    }
    $this->_redirectReferer();
} 
 
 
 
Step 5: Go to template/newsletter/subcriber.phtml

and copy the line
 <input type="text" name="country" id="country" title="<?php echo $this->__('Enter Country') ?>" class="input-text required-entry validate-email" />