1. Create a  “unsubscribe.phtml” page with the following code:

<?php $newsletterObj = new Mage_Newsletter_Block_Subscribe(); ?>
<div class="newsletter-unsubscribe">
    <div class="newsletter-unsubscribe-title"><?php echo $this->__('Submit your email id to unsubscribe newsletter') ?></div>
    <form action="<?php echo $newsletterObj->getUnsubscribeFormActionUrl() ?>" method="post" id="newsletter-validate-detail">
        <div class="block-content">
            <div class="input-box">
                <input type="text" name="email" id="newsletter" title="<?php echo $this->__('Sign up for our newsletter') ?>" class="input-text required-entry validate-email" value="<?php echo $this->__('Enter Your Email Here') ?>" onfocus="if(this.value=='<?php echo $this->__('Enter Your Email Here') ?>')this.value='';" onblur="if(this.value=='')this.value='<?php echo $this->__('Enter Your Email Here') ?>';"/>
            </div>
            <div class="actions">
                <button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Unsubscribe') ?></span></span></button>
            </div>
        </div>
    </form>
    <script type="text/javascript\">
        //<!&#91;CDATA&#91;
        var newsletterSubscriberFormDetail = new VarienForm('newsletter-validate-detail');
        //&#93;&#93;>
    </script>
</div>

2. Create a CMS page Name ‘Unsubscribe’ and url ‘unsubscribe’ and call that “unsubscribe.phtml” page in your cms page.
3. Now in page /app/design/frontend/your_package/your_theme/template/newsletter/subscribe.phtml add the follwing code to add a link to the cms page.

<div class="unsubscribe">
    <a href="<?php echo Mage::getUrl('unsubscribe') ?>"><?php echo $this->__('Unsubscribe') ?></a>
</div>

4.Now in /app/code/core/Mage/Newsletter/Block/Subscribe.php add a function to create the form action url which is called in the “unsubscribe.phtml”.

public function getUnsubscribeFormActionUrl()
    {
        return $this->getUrl('newsletter/subscriber/unsubscribecus', array('_secure' => true));
    }

5) Now in /app/code/core/Mage/Newsletter/controllers/SubscriberController.php page add new action for unsubscribe process.

/** * Unsubscribe newsletter from frontend */
public function unsubscribecusAction()
{
    $email = $this->getRequest()->getParam('email');
    $subsModel = Mage::getModel('newsletter/subscriber');
    $subscriber = $subsModel->loadByEmail($email);
    
    $id = (int) $subsModel->getId();
    $code = (string) $subsModel->getCode();
    if ($id && $code) {
    $session = Mage::getSingleton('core/session');
    try {
    Mage::getModel('newsletter/subscriber')->load($id)
    ->setCheckCode($code)
    ->unsubscribe();
    $session->addSuccess($this->__('You have been unsubscribed.'));
    }
    catch (Mage_Core_Exception $e) {
    $session->addException($e, $e->getMessage());
    }
    catch (Exception $e) {
    $session->addException($e, $this->__('There was a problem with the un-subscription.'));
    }
    }
    $this->_redirectReferer();
} 

Note: Remove Cache and refresh.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *