I added the lines at the top of the /template/newsletter/subscribe.phtml code below to only show the newsletter Subscribe field and button in the footer if the user is logged in AND if the user isn't subscribed yet.
This works well, but the problem is that after the user subscribes, the Subscribe field and button keep showing up, and only disappear once I flush the Magento cache in the backend. Logging out and in also does not refresh the subscription status. To me this indicates that once the subscription status is loaded, it isn't refreshed because it is in the cache. Does anyone know how I refresh it every time the page loads?
<!-- This checks whether the user is logged in --> <?php if ( Mage::getSingleton('customer/session')->isLoggedIn()): ?> <!-- This loads the user Newsletter subscription status --> <?php $customerSession = Mage::getSingleton("customer/session"); $email = $customerSession->getCustomer()->getEmail(); $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email); $status = $subscriber->isSubscribed(); // status = 1 if subscribed. ?> <!-- This only loads the Newsletter Subscribe field and button if the user isn't subscribed yet --> <?php if ($status == 0): ?> <div class="block block-subscribe"> <div class="block-title"> <strong><span><?php echo $this->__('Avails Alerts') ?></span></strong> </div> <form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="newsletter-validate-detail"> <div class="block-content"> <div class="form-subscribe-header"> <label for="newsletter"><?php echo $this->__('Sign Up for Avails Alerts:') ?></label> </div> <div class="input-box"> <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" /> </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> <?php endif; ?> <?php endif; ?>
Solved! Go to Solution.
then simply add this to default of your newsletter.xml
<reference name="footer"><action method="setCacheLifetime"></action></reference>
If you set the cache lifetime on the block to null in the layout xml on that block, it shouldn't be cached.
<newsletter_block_node> <!-- newsletter_block_node refers to whatever the actual node is in the layout xml file --> <action method="setCacheLifetime"><value>null</value></action> <!-- sets it to null, doesn't even try to fetch the cached block --> </newsletter_block_node>
edit: Modified the snippet to specifically send null
I tried adding your code to /layout/newsletter.xml (see below under the <default> node), but it just makes the Subscribe button disappear. I think I am not using the correct newsletter_block_node. What is the default newsletter_block_node name? Can you tell me how to add your snippet in that xml file?
<default> <!-- Mage_Newsletter --> <reference name="footer"> <block type="newsletter/subscribe" name="footer.newsletter" as="newsletter" before="footer_store_language" template="newsletter/subscribe.phtml"/> </reference> </default> <footer.newsletter> <!-- newsletter_block_node refers to whatever the actual node is in the layout xml file --> <action method="setCacheLifetime"><value>null</value></action> <!-- sets it to null, doesn't even try to fetch the cached block --> </footer.newsletter>
No you misunderstood. That <newsletter.footer> doesn't need to be there. This is what it should look like:
<default> <!-- Mage_Newsletter --> <reference name="footer"> <block type="newsletter/subscribe" name="footer.newsletter" as="newsletter" before="footer_store_language" template="newsletter/subscribe.phtml"> <action method="setCacheLifetime"><value>null</value></action> </block> </reference> </default>
Oh I see, thanks. Unfortunately, as soon as I add the "<action method=..." line within the block as you described, the newsletter block simply disappears. If I remove it, the newsletter block appears again. What could be the issue?
What happens if you switch it from null to 0? Also, have you tried looking at the system/exception logs?
It also disappears when I set it to 0 instead of null.
When I add that line, the system.log produces the following, presumably the cause for the block to disappear? - I am not sure what it means though...
2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 39: parser error : expected '>' in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450 2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: r/subscribe.phtml"> <action method="setCacheLifetime"><value>null</value></act in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450 2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: ^ in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450 2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 39: parser error : Opening and ending tag mismatch: action line 39 and act in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450 2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: r/subscribe.phtml"> <action method="setCacheLifetime"><value>null</value></act in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450 2015-10-28T04:55:25+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: ^ in /app/code/core/Mage/Core/Model/Layout/Update.php on line 450
That's perfect. Set it back to null and most importantly, fix the ending action tag. It looks like you only pasted
</act>
instead of
</action>
It fails because you have the opening action tag but not the proper ending action tag.
D'oh. I noticed once before that when I copy code from this forum, it seems to break the code in Magento, even though it looks like it pasted everything correctly into Notepad++... I typed the code instead, and now it doesn't break.
Alas, it has no effect on the frontend. I still need to flush the cache after I subscribe in order for the Subscribe field and button to disappear.
Should I maybe be using a different code to check the user Newsletter subscription status above?
Or should I add code somewhere to clear the variables ($status etc...)?
If I change the subscription on the My Account page, it instantly updates the subscription status after I save it - not sure why it doesn't do it here...
or
Do you have any other ideas how I could achieve this goal? I am not sure what else to try...