Magento Forum

   
Direct Newsletter Unsubscribe from Front-end by Customer himself
 
SandipanSaha
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2011-12-18
 

;*** An extension is developed for the feature ***

Available at http://www.magentocommerce.com/magento-connect/catalog/product/view/id/14602/

You can get the extension key on “Release Notes” tab.

Here I modified few codes to create the feature so that customer can directly unsubscribe newsletter from front-end.

1. Create a phtml page say “unsubscribe.phtml” containing the code to create the unsubscribe form.

<?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->__('Submit') ?></span></span></button>
</div>
</div>
</form>
<script type="text/javascript\">
//<![CDATA[
var newsletterSubscriberFormDetail = new VarienForm(’newsletter-validate-detail’);
//]]>
</script>
</div>

2. Create a CMS page. Add the just created block in it. So that your CMS page will contain that form.

3. Now in page \app\design\frontend\base\default\template\newsletter\subscribe.phtml add the code to add a link of the cms page.

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

4. In page \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();
}

Your modification complete. Enjoy!!

Please Note: I modified the core files. This is a wrong method if you want to update in future.

Image Attachments
newsletter1.JPGnewsletter2.JPG
 
Magento Community Magento Community
Magento Community
Magento Community
 
Cameron_iWeb
Member
 
Avatar
Total Posts:  62
Joined:  2012-03-22
 

Hi

You should simply copy the file you edit into your local/theme directory. This will overwrite the base file and keep magento stable in releases and have ability to upgrade.

Any files in your local folder or theme folders will not be overwritten unless you chose to.

e.g. 

\app\code\local\Mage\Newsletter\Block\Subscribe.php
\app\code\local\Mage\Newsletter\controllers\SubscriberController.php
and
\app\design\frontend\***MYTHEME**\default\template\newsletter\subscribe.phtml

This may help when developing themes in the future.

Thanks

Cameron

 
Magento Community Magento Community
Magento Community
Magento Community
 
SandipanSaha
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2011-12-18
 

Thanks cameron .. i use the theme folder for phtml files.. but was not sure about d core codes. will it work same as theme by copying files to local..??
im trying to learn how to override its core files..

 
Magento Community Magento Community
Magento Community
Magento Community
 
SandipanSaha
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2011-12-18
 

*** An extension is developed for the feature ***

Available at http://www.magentocommerce.com/magento-connect/catalog/product/view/id/14602/

You can get the extension key on “Release Notes” tab.

 
Magento Community Magento Community
Magento Community
Magento Community
 
krysis
Jr. Member
 
Total Posts:  6
Joined:  2013-01-23
 

Hi.
As I see it right there is no confirmation for the unsubscribe - correct?
So everyone who knows the email could unsubscribe another recipient without his knowledge.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top