1

I have to set some additional data to currently logged in customer session. I have tried setting up new data in following way, but it doesn't work.

if (Mage::getSingleton('customer/session')->isLoggedIn()):
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setTest1("test1_value");
$customer->setTest2("test2_value);
endif;

If I print the session right after I set new data to session,I can see the data, but when I refresh the page, the data (test1 and test2) disappears. Note: I can see other regular customer session data.

Please suggest how the session data can be set to customer data session. Looking forward, thank you in advance!

0

@Deepak Bjatta, I don't really see anything wrong with the code that you're showing, other than you could very well overwrite the data you're trying to set on each page load. Since the customer session has Varien_Object as an ancestor, you can do something like:

$customerSession = Mage::getSingleton('customer/session');
if ($customerSession->isLoggedIn()) {
    $customer = $customerSession->getCustomer();
    if (!$customer->hasTest1()){
        $customer->setTest1("some_value");
    } else {
        $customer->getTest1(); // do something with the value
        Mage::log($customer->getTest1()); // to see the output in var/log/system.log
    }
}

Re: Your ability to only print the data right after you set it, and not see the previously set data on page load...maybe you could show more code context? Maybe give the code above a shot and see if that does it for you?

  • Yes we are pretty much on same page. This does't help me. Can you try using your script in customer controller and test. Thank you for your reply. – Deepak Bhatta Jun 13 '16 at 10:37
  • @DeepakBhatta The code I pasted above was from a test that I ran locally, so that I was providing you with code I verified worked. – ryanF Jun 13 '16 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.