Customer session is different in different parts o

2020-08-01 06:00发布

问题:

I have a function inside of a Helper in Magento that returns whether or not a customer attribute equals one.

Here is my Helper class

class Nie_Nie_Helper_Data extends Mage_Core_Helper_Abstract {
    public function isNieAdmin() {
        if(Mage::getSingleton('customer/session')->getCustomer()->getNieAdmin() == 1) {
            return true;
        } else {
            return false;
        }
    }
}

Now when I call this function from a class that extends Mage_Core_Block_Template, everything seems to work fine. However when I try to use this inside one of my controllers, it does not work. In fact when I do Mage::getSingleton('customer/session')->getCustomer()->debug() the only variable that is returned is the website_id.

Does anyone know what I have to do in order to get this to work?

回答1:

At the time of the controller the session objects are not yet initialised (although the session variable must be) so it returns a blank model. My guess is the website_id is deliberately set in the creation of a customer object to act as a default.

You could access $_SESSION['customer'] directly to find what you need, but that is messy. An alternative would be to do what you want in an event that occurs later.

I hope someone can come up with a better answer than mine.



回答2:

Ok it looks like I had to load up the session myself. I had to put the following in my functions:

Mage::getSingleton('core/session', array('name' => 'frontend'));

Hope this helps.



标签: magento