2

I'm trying to set a session variable so a pop-up box only appears once per session.

To do this I've created a module that is called via AJAX when someone closes the popup. The module's controller sets a session variable. The controller looks like this:

class Company_Popup_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        Mage::getSingleton('core/session')->setNewsletterPopup('yes');
    }
}

Super simple.

Then, in the block I check if the session variable is set (or if the user is logged in):

class Company_Popup_Block_Popup extends Mage_Core_Block_Template
{
    public function displayPopup(){
        if(!$this->helper('customer')->isLoggedIn() && Mage::getSingleton('core/session')->getNewsletterPopup() == null){
            return true;
        }
        else{
            return false;
        }
    }
    public function getSessionData(){
        return Mage::getSingleton('core/session');
    }
}

The template file then calls the displayPopup method and either displays the newsletter popup or not.

It all works perfectly... until I turn on Cache at which point the session variable doesn't get set or at the least isn't accessible in the block / template file.

What have I missed here?

6
  • Where are you rendering your block, e.g. is it in footer.phtml? Commented Oct 10, 2012 at 16:02
  • are you running CE or EE edition? Commented Oct 10, 2012 at 16:17
  • it could be simpler if you simply use javascript and cookies witch are not cached. you can set a cookie for the current session only... Commented Oct 10, 2012 at 16:21
  • @Goonanism You'll need to be a little more specific, "turn on cache" means a lot of different things in Magento. What Magento version are you using, what "cache" did you turn on. If you're not sure on the later, what steps did you take to "turn on" the cache? Commented Oct 10, 2012 at 18:06
  • Thanks all for your comments. A few points of clarification. I'm using 1.7 CE and rendering the block in the footer.phtml. I've tried javascript cookies and it was a bit of a nightmare. By 'turn cache on' I mean going into cache storage management and enabling all cache type. Commented Oct 10, 2012 at 21:34

1 Answer 1

1

I imagine the block is being cached, which means the updated session variable isn't accessed, rather the original call which has been cached is returned.

You should try and disable cache inside the block.

Try adding this into your block:

 public function getCacheLifetime() { return null; } 

this will disable the caching on your block and should allow it to be called correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for good information, but I dont think this is it. The entire block code is up there, and there's no explicit cache set, which means the block won't be cached as HTML output. More likely it's the full page cache skipping the controller action that's doing it. (but hard to tell either way without more information from the original poster)
Hi @Andrew, I just tried your suggestion and it didn't work I'm afraid. As Alan says, I haven't set any explicit cache anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.