PHP Magento Tips

Display Newsletter Subscription Messages in footer in Magento

Rate this posting:
{[['']]}

In Magento, Newsletter Subscription block was located in the footer, so, why should the subscribers see the messages of their subscription success (or failure) at the top of the page? Have you ever think of that?

to solve this, we can not move the whole messages block to the footer because it will contain the other messages: login failure e.g. Furthermore, the subscription and other messages can appear on the same page and at the same time, so we should think about separating different core messages types.

For that i have created one simple module which will override Mage_Newsletter_SubscriberController.

Step 1: Create file in
app/etc/modules/Mycustom_Newsletter.xml and add below code in it.

<?xml version="1.0"?>
<config>
 <modules>
  <Mycustom_Newsletter>
   <active>true</active>
   <codePool>local</codePool>
  </Mycustom_Newsletter>
 </modules>
</config>

Step 2: Create config.xml file in app/code/local/Mycustom/Newsletter/etc/config.xml and add below code in it.

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <modules>
  <Mycustom_Newsletter>
   <version>0.1.0</version>
  </Mycustom_Newsletter>
 </modules>
 <global>
  <blocks>
   <mycustom_newsletter>
    <class>Mycustom_Newsletter_Block</class>
   </mycustom_newsletter>
   <core>
    <rewrite>
     <messages>Mycustom_Newsletter_Block_Core_Messages</messages>
    </rewrite>
   </core>
  </blocks>
 </global>
 <frontend>
  <routers>
   <newsletter>
    <args>
     <modules>
      <Mycustom_Newsletter before="Mage_Newsletter_SubscriberController">Mycustom_Newsletter</Mycustom_Newsletter>
     </modules>
    </args>
   </newsletter>
  </routers>
  <layout>
      <updates>
          <mycustom_newsletter>
              <file>mycustom_newsletter.xml</file>
          </mycustom_newsletter>
      </updates>
  </layout>
 </frontend>
</config>

Step 3: Create file in app/code/local/Mycustom/Newsletter/controllers/SubscriberController.php and add below code in it.

<?php
include_once ("Mage/Newsletter/controllers/SubscriberController.php");
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' )->

    setWebsiteId ( Mage::app ()->getStore ()->getWebsiteId () )->

    loadByEmail ( $email )->

    getId ();
    
    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 {
     
     $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.' ) );
   }
  }
  // Adding class 'subscription' for further messages filtration
  $session->getMessages ()->getLastAddedMessage ()->setIdentifier ( 'subscription' );
  $this->_redirectReferer ();
 }
}

Step 4: Create file in app/code/local/Mycustom/Newsletter/Block/Core/Messages.php and add below code in it.

For reference check app/code/core/Mage/Core/Block/Messages.php file and check addMessages and _prepareLayout functions in it.

So, here we simply update addMessages() method not to add messages with a desired identifier to the current collection and updated _prepareLayout method changing getMessages() method call parameter to FALSE. This means that messages session will not be cleared after the messages extraction. Also, pay attention please, that we called original Mage_Core_Block_Template::_prepareLayout() instead of calling parent::_prepareLayout(), which could destroy our session.


<?php
class Mycustom_Newsletter_Block_Core_Messages extends Mage_Core_Block_Messages
{
    public function _prepareLayout()
    {
        $this->addMessages(Mage::getSingleton('core/session')->getMessages(false));
        Mage_Core_Block_Template::_prepareLayout();
    }
    /**
     * Add messages to display
     *
     * @param Mage_Core_Model_Message_Collection $messages
     * @return Mage_Core_Block_Messages
     */
    public function addMessages(Mage_Core_Model_Message_Collection $messages)
    {
        foreach ($messages->getItems() as $message) {
            if($message->getIdentifier() != 'subscription')
            {
                $this->getMessageCollection()->add($message);
            }
        }
        return $this;
    }
}


Step 5: Create file in app/code/local/Mycustom/Newsletter/Block/Subscription/Messages.php and add below code in it.

Everything is pretty same here, except of adding ONLY desired messages to the messages collection and unsetting messages session in _prepareLayout() method. If you want to use more then two places for messages output, simply put TRUE on getMessages only in the LAST block you call.

<?php
class Mycustom_Newsletter_Block_Subscription_Messages extends Mage_Core_Block_Messages
{
    public function _prepareLayout()
    {
        $this->addMessages(Mage::getSingleton('core/session')->getMessages(true));
        Mage_Core_Block_Template::_prepareLayout();
    }
    /**
     * Add subscription messages to display
     *
     * @param Mage_Core_Model_Message_Collection $messages
     * @return Mage_Core_Block_Messages
     */
    public function addMessages(Mage_Core_Model_Message_Collection $messages)
    {
        foreach ($messages->getItems() as $message) {
            if($message->getIdentifier() == 'subscription')
            {
                $this->getMessageCollection()->add($message);
            }
        }
        return $this;
    }
}

Step 6: Create file in app/design/frontend/rwd/default/layout/mycustom_newsletter.xml and add below code in it.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="footer.newsletter">
            <block type="mycustom_newsletter/subscription_messages" name="subscription_messages" />
        </reference>
    </default>
</layout>

Step 7 : Modify in app/design/frontend/rwd/default/template/newsletter/subscribe.phtml and add
 <?php echo $this->getChildHtml('subscription_messages') ?>
in it.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="block block-subscribe">
    <div class="block-title">
        <strong><span><?php echo $this->__('Newsletter') ?></span></strong>
    </div>
    <form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="newsletter-validate-detail">
        <div class="block-content">
         <?php echo $this->getChildHtml('subscription_messages') ?>         
            <div class="form-subscribe-header">
                <label for="newsletter"><?php echo $this->__('Sign Up for Our Newsletter:') ?></label>
            </div>
            <div class="input-box">
               <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email" id="newsletter" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Sign up for our newsletter')) ?>" class="input-text required-entry validate-email" />
            </div>
            <div class="actions">
                <button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Subscribe')) ?>" class="button"><span><span><?php echo $this->__('Subscribe') ?></span></span></button>
            </div>
        </div>
    </form>
    <script type="text/javascript">
    //<![CDATA[
        var newsletterSubscriberFormDetail = new VarienForm('newsletter-validate-detail');
    //]]>
    </script>
</div>

Download Complete Module from Github: https://github.com/vijayrami/Newsletter_Bottom_Messages

You can now check Global messages on front end as per below.


0 Comments On "Display Newsletter Subscription Messages in footer in Magento"

Back To Top