这个问题首先被意识到是因为前端的订单历史页面(*/sales/order/history/)只显示一条短消息:您没有下订单。
经过一番调试,发现问题出在这个函数上:
Mage::getSingleton('customer/session')
它不会返回包含除网站 ID 之外的当前客户信息的会话实体,这就是订单集合在筛选客户 ID 后返回空结果的原因。
Stack Overflow 上实际上有一篇文章讨论了类似的问题:Magento 网站不同部分的客户会话是不同的 https://stackoverflow.com/questions/4718659/customer-session-is-different-in-different-parts-of-a-magento-website。但并没有给出很好的解释。
令人困惑的是,就我的情况而言,该函数在某些部分运行良好,但在其他部分则不然。例如,我插入了
<?php echo var_export(Mage::getSingleton('customer/session')->getCustomerId(), true) ?>
进入目录产品列表模板,登录后显示客户ID。但同一行在订单历史记录页面中返回 Null。
我在 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;
}
设置网站 id 后,它会根据 $this->getId() 加载客户实体。
为什么客户会话实体的“id”属性并不总是有效?有人可以分享他的知识吗?非常感谢。