Tiago Sampaio

main ai engineering career

engineering

2 min read

Erro ao Carregar um Cliente no Magento: Customer website ID must be specified when using the website scope.

Estes dias estava desenvolvendo uma nova funcionalidade em um dos módulos no qual estava trabalhando e me deparei com o seguinte erro:

‘Customer website ID must be specified when using the website scope.’

Pois bem, isso acontecia no momento que eu tentava utilizar um método do objeto Customer do Magento (Mage_Customer_Model_Customer) chamado loadById(), que retorna um cliente a partir de um e-mail:

[php]
$emailReferral = ‘[email protected]’;
$referer = Mage::getModel(‘customer/customer’)->loadByEmail($emailReferral);
[/php]

Procurei um pouco e rapidamente identifiquei no método que o erro era disparado no momento em que o Magento tentava capturar a informação ‘website_id’ do Objeto, veja no código abaixo:

Mage_Customer_Model_Customer::loadByEmail()

[php]
/**
* Load customer by email
*
* @param Mage_Customer_Model_Customer $customer
* @param string $email
* @param bool $testOnly
* @return Mage_Customer_Model_Entity_Customer
* @throws Mage_Core_Exception
*/
public function loadByEmail(Mage_Customer_Model_Customer $customer, $email, $testOnly = false)
{
$select = $this->_getReadAdapter()->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
//->where(’email=?’, $email);
->where(’email=:customer_email’);
if ($customer->getSharingConfig()->isWebsiteScope()) {
if (!$customer->hasData(‘website_id’)) {
Mage::throwException(Mage::helper(‘customer’)->__(‘Customer website ID must be specified when using the website scope.’));
}
$select->where(‘website_id=?’, (int)$customer->getWebsiteId());
}

if ($id = $this->_getReadAdapter()->fetchOne($select, array(‘customer_email’ => $email))) {
$this->load($customer, $id);
}
else {
$customer->setData(array());
}
return $this;
}
[/php]

Repare no trecho deste método em que existe um teste para verificar se existe a informação ‘website_id’:

[php]
if (!$customer->hasData(‘website_id’)) {
Mage::throwException(Mage::helper(‘customer’)->__(‘Customer website ID must be specified when using the website scope.’));
}
[/php]

A resolução para este erro é mais simples do que você acha, pois uma vez que o método existe que exista esta informação no objeto basta incluirmos a informação antes de fazer o load do objeto:

[php]
$emailReferral = ‘[email protected]’;
$referer = Mage::getModel(‘customer/customer’)->setWebsiteId(Mage::app()->getWebsite()->getId())->loadByEmail($emailReferral);
[/php]

Neste trecho incluímos o método setWebsiteId() logo após o método getModel da classe Mage. Definimos o valor, que é o id do Website atual e tudo volta a funcionar novamente!

Espero que isto tenha ajudado vocês.

Um grande abraço e até a próxima!

Tiago Sampaio

Written by

TS

Tiago Sampaio

Adobe Commerce & Magento 2 Expert

Senior developer with 15+ years building software. Writes practical fixes for Magento 2 and Adobe Commerce, and honest notes on growing as a developer.

$ git remote add inbox

Get new commits by email

New posts from both branches, engineering and career. No spam, unsubscribe any time.