cancel
Showing results for 
Search instead for 
Did you mean: 

Add Newsletter Subscribe button for registered users

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Add Newsletter Subscribe button for registered users

I would like to add a "Subscribe to Newsletter" button to the home page which lets a registered user subscribe with one click, without having to enter their e-mail address again (since they already provided their e-mail address when they registered). How can I do this?

 

P.S. I am hiding Magento's standard Newsletter subscription box.

8 REPLIES 8

Re: Add Newsletter Subscribe button for registered users

Something like this should get you started:

<?php if ( Mage::getSingleton('customer/session')->isLoggedIn()): ?>
	<input type="text" name="newsletter" value="<?php echo Mage::getSingleton('customer/session')->getCustomer()->getEmail() ?>" />
<?php endif; ?>

Re: Add Newsletter Subscribe button for registered users

Thanks mrhambley!

 

I vaguely follow your code, but how do I work this into a button on the front end? Sorry, I am not well-versed in Magento programming and would appreciate your help!

Re: Add Newsletter Subscribe button for registered users

Hi mrhambley, I tried to put this code in the header or footer.phtml, but if I do that, it breaks the site, and I get a "There has been an error processing your request" error... Can you tell me how i properly use this code? Thanks!

Re: Add Newsletter Subscribe button for registered users

Actually I now was able to load the customer e-mail address in subscribe.phtml by changing

 

<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"  />

to

 

<input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email" id="newsletter" value="<?php echo Mage::getSingleton('customer/session')->getCustomer()->getEmail() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Sign up for our newsletter')) ?>" class="input-text required-entry validate-email hide-input-field"  />

also hiding the input field with an additional CSS class added at the end, so only the Subscribe button appears.

 

 

I then wrapped the entire subscribe.phtml in this

<?php if ( Mage::getSingleton('customer/session')->isLoggedIn()): ?>


<?php endif; ?>

so the subscribe button only appears if the customer is logged in.

 

 

So that worked great, thanks for the pointer mrhambley!

Re: Add Newsletter Subscribe button for registered users

However, now I would also like to hide the Subscribe button if the logged-in in customer is already subscribed.

 

If I check the code for the customer account dashboard which states whether the customer is subscribed or not, I see the code:

 

<?php if( $this->getIsSubscribed() ): ?>

If I put this right after the line that checks whether the customer is logged in in subscribe.phtml, it doesn't do anything, i.e. the Subscribe button still displays even if I am already subscribed. I also tried to change it to something like:

 

<?php if ( Mage::getSingleton('customer/session')->getCustomer()->getEmail()->getIsSubscribed() ): ?>

But that doesn't seem to work either.

 

Does anyone have the correct code?

Re: Add Newsletter Subscribe button for registered users

Ok I found some code on Stackexchange that works. So now at the beginning of subscribe.phtml it says:

 

<?php if ( Mage::getSingleton('customer/session')->isLoggedIn()): ?>

<?php
$customerSession = Mage::getSingleton("customer/session");
$email = $customerSession->getCustomer()->getEmail();
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
    $status = $subscriber->isSubscribed(); // status = 1 if subscribed.
?>

<?php if ($status == 0): ?>

 

followed by all of the default subscribe.phtml code. Now the subscribe button is shown only if logged in and only if unsubscribed - BUT: after I subscribe, the Subscribe button does not disappear until I flush the cache in the Magento backend.

 

Do I need to clear some of the variables above once the customer clicks on the Subscribe button? How do I do that?

Re: Add Newsletter Subscribe button for registered users

Does anybody have any idea?

Do I have the wrong approach in how I read the subscription status?

If go the the Account page and check/uncheck the subscription to the newsletter, it recognizes the change right away after I save it, but I can't figure out how...

Re: Add Newsletter Subscribe button for registered users

Hello,

 

This issue is related to Magento Cache, please follow here: https://community.magento.com/t5/Programming-Questions/Refresh-user-newsletter-subscription-status/t...

Problem solved? Click Accept as Solution!