0

I added a newsletter form on a CMS page and I created a new template to show the form. I was wondering if there is a way to display a custom success message for just that one form?

It seems I remember seeing a way by using javascript but I can't find it.

1

To do it clean, the success newsletter message is stored in newsletter controller : Mage_Newsletter_SubscriberController > newAction()function > $session->addSuccess($this->__('Thank you for your subscription.'));.

So if you want to customize this message just for one form your have to this:

1) Rewrite the controller in your local, from : app/code/core/Mage/Newsletter/controllers/SubscriberController.php to your local codepool.

2) Create a new function, you call it for example customnewAction():

public function customnewAction()
{
    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->__('YOUR CUSTOM MESSAGE HERE.'));
            }
        }
        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();
}

3) Create your custom Newsletter template in : app/design/frontend/{package}/{theme}/template/newsletter/custom_subscribe.phtml then put inside the code bellow:

<div class="block block-subscribe">
    <div class="block-title">
        <strong><span><?php echo $this->__('Newsletter') ?></span></strong>
    </div>
    <form action="<?php echo $this->getUrl('newsletter/subscriber/customnew'); ?>" method="post" id="newsletter-validate-detail">
        <div class="block-content">
            <div class="form-subscribe-header">
                <label for="newsletter"><?php echo $this->__('Sign Up for Our Newsletter:') ?></label>
            </div>
            <div class="input-box">
               <input type="text" 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>

nb: the form action of this custom newsletter call the new function that we created in step 2) <form action="<?php echo $this->getUrl('newsletter/subscriber/customnew'); ?> ...

4) If you want to display this new Newsletter form in your CMS page you have to put this code inside:

{{block type="newsletter/subscribe" name="custom.newsletter" template="newsletter/custom_subscribe.phtml"}} 

5) Clear your Magento cache and you'll get what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.