Magento - getCustomer()给出了意想不到的结果

时间:2016-01-18 16:48:16

标签: php magento

使用Magento时,我遇到了一些奇怪的结果

Mage::getSingleton('customer/session')->getCustomer();

我有以下代码首先检查客户是否已登录,如果是,则获取登录客户的名字:

if(!$this->helper('customer')->isLoggedIn())
{
    $name = Mage::getSingleton('customer/session')->getCustomer()->getFirstname();
    print 'Hello '.$name;
    // ...
}

大部分时间这都可以正常工作,如果登录Joe Bloggs,则会输出:

Hello Joe

但是时不时地,似乎是当大量客户登录时,我会得到其他名称的意外输出

Hello Lucy 
or
Hello John

Mage::getSingleton('customer/session')->getCustomer()是获取客户详细信息的一种万无一失的方式,还是有可能获得另一个登录客户详细信息?或者我的会话混乱有问题吗?

1 个答案:

答案 0 :(得分:0)

根据Mage_Customer_Model_Session::getCustomer中的核心代码:

/**
 * Retrieve customer model object
 *
 * @return Mage_Customer_Model_Customer
 */
public function getCustomer()
{
    if ($this->_customer instanceof Mage_Customer_Model_Customer) {
        return $this->_customer;
    }

    $customer = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
    if ($this->getId()) {
        $customer->load($this->getId());
    }

    $this->setCustomer($customer);
    return $this->_customer;
}

......无法加载其他客户的数据(由核心Magento提供)。

如果客户模型已经加载,它将首先返回客户模型,如果没有,则在已经存在ID的情况下加载它,最后如果没有别的话,它将返回空的客户模型。

您的会话可能会变得混乱。我建议你检查你的配置 - 你是否使用数据库,文件,Redis等进行会话存储?

相关问题