2

This problem first came into awareness as the order histories page (*/sales/order/history/) in frontend was displaying nothing but a short message: You have placed no orders.

After some debugging, it turns out the problem lies with this function:

Mage::getSingleton('customer/session')

It does not return a session entity containing current customer's information except the website id, which is why the order collection returns an empty result after filtering on the customer id.

There is actually a post on Stack Overflow discussing a similar problem: Customer session is different in different parts of a Magento website . But it has not given a good explaination.

And what is confusing is that, with my case, the function works well in some parts but not the others. For example, I inserted

<?php echo var_export(Mage::getSingleton('customer/session')->getCustomerId(), true) ?>  

into the catalog product list template, and it displays the customer id after login. But the same line returns Null in the order histories page.

I located these codes in app/code/core/Mage/Customer/Model/Session.php

public function getCustomer()
{
    ...
    $customer = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
    if ($this->getId()) {
        $customer->load($this->getId());
    }
    $this->setCustomer($customer);
    return $this->_customer;
}

After setting the website id, it loads the customer entity depending on $this->getId().

Why the 'id' attribute of customer session entity is not always valid? Can someone share his knowledge please. Many thanks.

Community
  • 1
  • 1
vicch
  • 696
  • 2
  • 6
  • 13
  • Is the order history page HTTPS whereas the catalog page HTTP? It's possible that the session isn't being passed properly between the two. – nachito Mar 27 '12 at 16:18
  • No, they are all using HTTP. And telling from the packages, the requests to these pages are identical except the requested URLs and referers. – vicch Mar 28 '12 at 07:54
  • In fact on /sales/order/history/ page, the customer is not 'treated' as logged in. There isn't the 'log out' link displayed either. What could be the problem if the customer is seen as not logged in on some pages? – vicch Mar 28 '12 at 11:02
  • Check your error logs for any clues. Note that `session_start()` must be called before any output is sent to the browser, so if there was an error it could be preventing the session from being started. – nachito Mar 28 '12 at 14:06

4 Answers4

1

If you are using Google Chrome, you can right-click on the page, choose Inspect Element, then Resources tab, look for Cookies down the left-hand menu, and click on your domain name.

Under the domain name, the cookies that have been set for it will be displayed, along with path, expiry, etc. Very useful.

In your case, you are looking for the 'frontend' session cookie under 'name'. You can delete it, and refresh the page to see if Magento recreates it, in which case sessions are probs working. Hope that helps :)

PS: Magento uses sessions very heavily, if they're broken other things will be breaking all over the show as well.

Jongosi
  • 2,305
  • 1
  • 28
  • 31
  • In my case, there were 2 adminhtml values, which were exactly the same, but I think the system got confused. In any event, this comment helped me save a lot of headache. – DWils Jan 23 '15 at 00:58
  • @ DWils if you look carefully, you probably have one cookie for `domain.com` and another for `.domain.com`. This allows cookies for the domain, and all subdomains, secure and insecure. You can turn that off in **System > Config > [General] > Web > Session Cookie Management**. Clear the fields **Cookie Path** and **Cookie Domain**. – Jongosi Feb 10 '16 at 01:51
1

It's interesting why the user is not get redirected to the login page (customer/account/login/) when the customer session is empty and you are visiting the history page. Please verify if Mage_Customer or Mage_Sales were customized.

If it was rewritten this would be the first place to check.

1

I'd be careful with Magento when assuming that the problem lies with one particular area. You can head down a pretty deep rabbit hole before realising you need to start all over.

If you're accurate in that the problem lies with the session getter, try this:

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

But I would also keep looking at other options. Sounds similar to the problems a lot of people have with caching. Consider trying the same with all caching switched off. The full page cache can do unexpected things to variable values.

What file are you echoing from in your example? I'd need to know this before helping any further.

Magento Guy
  • 2,493
  • 1
  • 16
  • 13
  • I think the problem comes from the dispatching process which has been rewritten in custom modules. Need more efforts to make things clear. – vicch Apr 03 '12 at 14:48
0

I'm going to blast this answer to some of these Mage questions because no one mentions the cookies could be incorrectly setup. We wasted a morning on this after we imported a DB update that had the wrong cookie domain.

Make sure you core_config_data table has the right setting for this path value:

web/cookie/cookie_domain

The domain should match what you have in the URL.

https://www.myawesomemagentosite.com

web/cookie/cookie_domain = .myawesomemagentosite.com
akahunahi
  • 1,782
  • 23
  • 21