6

I am developing custom module which needs to use customer data from session. Here I am having some problem with session. I am unable to get any customer data in my custom module from session even if the user logged in.

I logged below code in my index controller.

Mage::getSingleton('core/session', array('name' => 'frontend'));
 $customer_id = Mage::getSingleton('customer/session')->getCustomer()->getId();
 Mage::log("Index - customer id check: " . $customer_id);

A failure to get customer Id.

After that added customer_login event and tried to log the same in the observer method. Here it worked.

But, still not working in the index controller.

Please help

Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
asked Jun 29, 2015 at 14:58

2 Answers 2

10

You need to check customer is loggedin or not.

Without customer loggin,you cannot get customer data from session:

if (Mage::getSingleton('customer/session')->isLoggedIn()):
 $customer = Mage::getSingleton('customer/session')->getCustomer();
 $customer->getEmail();
 $customer->getFirstname();
 $customer->getLastname();
 $customer->getId();
endif;
answered Jun 29, 2015 at 18:21
2
  • Thanks Amit, I checked with your code, but still I am not getting the customer details in my custom module index controller. Commented Jun 29, 2015 at 19:10
  • Are controllers work? Commented Jun 29, 2015 at 19:12
3

Try below code

 if (Mage::getModel('customer/session')->isLoggedIn()) {
 $customer = Mage::getSingleton('customer/session')->getCustomer();
 echo '<pre>';
 print_r($customer->getData());
 } else {
 echo 'Not Logged in';
 }
answered Aug 24, 2015 at 9:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.