magento报错:Customer website ID must be specified when using the website scope

根据邮件用户名获取顾客对象时,可以用Customer模块自带的loadByEmail方法

app/code/core/Mage/Customer/Model/Customer.php
1
2
3
4
5
public function loadByEmail($customerEmail)
{
$this->_getResource()->loadByEmail($this, $customerEmail);
return $this;
}

但是直接使用下面的方法

1
2
$customer = Mage::getModel('customer/customer')
->loadByEmail($email);

会报错,错误信息如下:

Customer website ID must be specified when using the website

事实上,创建customer对象时需要指定websitId

正确的使用方法如下:

1
2
3
$customer = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->loadByEmail($email);