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" />

10 comments:

  1. Thank you very much. It work for me when I override magento default newsletter extension.

    ReplyDelete
  2. Thanks for your review. :)

    ReplyDelete
  3. it's a great tutorial!!! I guess front-end implementation is missing. Could you also mention to add the field in newsletter form in front-end?

    ReplyDelete
  4. I updated the Post. Kindly follow Step 5 For Front-end Implementation.

    ReplyDelete
  5. Sorry For the late reply...thanks

    ReplyDelete
  6. great post. How do you get to add the extra field(s) to the customer account section. Customers can edit there preferences from the Dashboard once they log in. What code do you need on template/customer/form/newsletter.phtml?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I tried it with Magento v1.9.2.4 but its not work for me.

    ReplyDelete
  9. I am constantly surprised by the amount of information accessible on this subject. What you presented was well researched and well written to get your stand on this over to all your readers. Thanks a lot my dear. magento eshop

    ReplyDelete