PHP Magento Tips

Powered by Blogger.

How to Override Newsletter Controllers Magento

Rate this posting:

Overriding Magento core controller is not as simple as you do by moving app/code/core files to app/code/local. To override magento core controller, you have to create custom module and some required files in it. Let’s start by overriding newsletter subscriber controller located in app/code/core/Mage/Newsletter/controllers/SubscriberController.php

Create Basic Module Structure:

Here's the list of files required to create your basic module to override any core controller of Magento:

app/etc/modules/Mycustom_Newsletter.xml: It's a file used to enable our custom module.

app/code/local/Mycustom/Newsletter/etc/config.xml: It's a module configuration file in which we'll set up controller class overriding using certain tags as per the Magento conventions.

app/code/local/Mycustom/Newsletter/controllers/SubscriberController.php: It's a custom controller class file which we'll use to override the core "SubscriberController" file.

Step 1:

First, we need to create a module enabler file. Create a file "app/etc/modules/ Mycustom_Newsletter.xml" and paste the following contents in that file. We've used Mycustom  as our module namespace and Newsletter  as our module name. It'll enable our "Newsletter" module by default.

<?xml version="1.0"?>









Step 2:

Next, we need to create a module configuration file. Create "app/code/local/Mycustom/Newsletter/etc/config.xml" and paste the following contents in that file.

<?xml version="1.0" encoding="UTF-8"?>












                        <Mycustom_Newsletter before="Mage_Newsletter_SubscriberController">Mycustom_Newsletter</Mycustom_Newsletter>







First, we've defined a module version number using the <version>  tag. The <newsletter> tag is the front controller router name of the "Newsletter" core module. It informs the routing system that we want to override the controllers of the "Newsletter" module.

Finally, the < Mycustom_Newsletter before="Mage_Newsletter_SubscriberController">Mycustom_Newsletter</ Mycustom_Newsletter>
tag is used to inform the Magento routing system that it should load your custom module's controller files if available. Of course, it'll load the core files of the "Newsletter" module if it isn't able to find them under your module.


Finally, define your controller class Mycustom_Newsletter_SubscriberController. Let's create a controller file "app/code/local/Mycustom/ Newsletter /controllers/ SubscriberController.php". Here You can define your own new action or modify(override) code of existing action.

Suppose we want to add new subscribed email address into mailchimp list account, then we have to modify "newaction" for that. Also you use ebizmarts - MageMonkey - Official MailChimp and Mandrill Extension for that. But for simple use we can do the same process with Curl. Check highlighted code below.



class Mycustom_Newsletter_SubscriberController extends Mage_Newsletter_SubscriberController {

public function newAction()


        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {

            $session            = Mage::getSingleton('core/session');

            $customerSession    = Mage::getSingleton('customer/session');

            $email              = (string) $this->getRequest()->getPost('email');

            try {

                if (!Zend_Validate::is($email, 'EmailAddress')) {

                    Mage::throwException($this->__('Please enter a valid email address.'));


                if (Mage::getStoreConfig(Mage_Newsletter_Model_Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG) != 1 &&

                    !$customerSession->isLoggedIn()) {

                    Mage::throwException($this->__('Sorry, but administrator denied subscription for guests. Please <a href="%s">register</a>.', Mage::helper('customer')->getRegisterUrl()));


                $ownerId = Mage::getModel('customer/customer')




                if ($ownerId !== null && $ownerId != $customerSession->getId()) {

                    Mage::throwException($this->__('This email address is already assigned to another user.'));


                $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 {

                function mc_subscribe($email, $fname, $apikey) {    

                //this works only if you installed Magento Mailchimp Extension. or Directly enter you 'API key' here.
                $apikey  = Mage::helper('monkey')->getApiKey($store);

                //this works only if you installed Magento Mailchimp Extension. or Enter you List id Here.
                $listid = Mage::helper('monkey')->getDefaultList(Mage::app()->getStore()->getId());

                $fname = '';

                $serverarray = explode("-", $apikey);
                $server = $serverarray[1];
                  //check data start
                  $flag = 'false';
                  $checkurl = 'https://'.$server.''.$listid.'/members?apikey='.$apikey.'&fields=members.email_address&status=subscribed';
                  //  Initiate curl
                  $ch = curl_init();
                  // Disable SSL verification
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                  // Will return the response, if false it print the response
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                  // Set the url
                  curl_setopt($ch, CURLOPT_URL,$checkurl);
                  // Execute
                  // Closing

                  // Will dump a beauty json :3
                  $getmember = json_decode($result, true);

                  $myarray = array_values($getmember);

                  $myresult = $myarray[0];

                  foreach ($myresult as $myvalue){
                    foreach ($myvalue as $key=>$value){
                               $v[] .= $value;
                  $haystack = array_values($v);
                  $needle = $email;

                  if (in_array($needle, $haystack)) {
                       $flag = 'true';
                  } else {
                       $flag = 'false';
                  if ($flag == 'false'){
                  //check data Ends 
                $auth = base64_encode( 'user:'.$apikey );

                $data = array(

                'apikey'        => $apikey,

                'email_address' => $email,

                'status'        => 'subscribed',

                'merge_fields'  => array(

                'FNAME' => $fname



                $json_data = json_encode($data);

                $ch = curl_init();


                curl_setopt($ch, CURLOPT_URL, 'https://'.$server.''.$listid.'/members/?apikey='.$apikey);

                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',

                'Authorization: Basic '.$auth));

                curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_TIMEOUT, 10);

                curl_setopt($ch, CURLOPT_POST, true);

                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

                $result = curl_exec($ch);




                mc_subscribe($email, $fname, $apikey);

                    $session->addSuccess($this->__('Thank you for your subscription.'));



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





5 Comments On "How to Override Newsletter Controllers Magento"

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
ecommerce website development company | ECommerce Development Company in BangaloreEcommerce Website Design and Development Company in Bangalore | Magento Development Company

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Typo3, kindly contact us
MaxMunus Offer World Class Virtual Instructor led training on Typo3. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email :
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –

Thank you for sharing excellent information. Your website is very cool. Fully useful your blog post... Online Shopping Website In India

Not working for me... pls help me

Thank You

Not working for me... Pls help me

Thank You

Back To Top