2

I have a problem in Magento.

I currently have multistores for both Wholessale and retail. The problem is that my stores allow duplicate emails over the different stores. For example, a wholesale customer could sign up to retail using their existing wholesale email. We will need this fixed. At the minute, Magento is only blocking duplicate emails from the same store view but does not check over all stores.

I have found a block of code below but cannot seem to edit it so that it will do a check over all stores. Would someone on here be willing to help me? See code below:

protected function _beforeSave(Varien_Object $customer)
{
parent::_beforeSave($customer);

if (!$customer->getEmail()) {
    throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
}

$adapter = $this->_getWriteAdapter();
$bind    = array('email' => $customer->getEmail());

$select = $adapter->select()
    ->from($this->getEntityTable(), array($this->getEntityIdField()))
    ->where('email = :email');
if ($customer->getSharingConfig()->isWebsiteScope()) {
    $bind['website_id'] = (int)$customer->getWebsiteId();
    $select->where('website_id = :website_id');
}
if ($customer->getId()) {
    $bind['entity_id'] = (int)$customer->getId();
    $select->where('entity_id != :entity_id');
}

$result = $adapter->fetchOne($select, $bind);
if ($result) {
    throw Mage::exception(
        'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
        Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
    );
}

// set confirmation key logic
if ($customer->getForceConfirmed()) {
    $customer->setConfirmation(null);
} elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
    $customer->setConfirmation($customer->getRandomConfirmationKey());
}
// remove customer confirmation key from database, if empty
if (!$customer->getConfirmation()) {
    $customer->setConfirmation(null);
}

return $this;
}

2 Answers 2

1

Try this for loading the customer, I think is better to use magento models instead of writing your own selects:

$customer = Mage::getModel('customer/customer');
$customer->loadByEmail($login['email']);

In your code you are doing this:

$select->where('website_id = :website_id');

That is restricting the select only to the website_id you provide Basically what you need is this query:

select * from customer_entity where email = '[email protected]';

Then you can check like this:

if($customer->hasData())

And you know that the email provided is already registered or not.

Update

So if you want to prevent customers to register with an already existing email, regardless store_id or website_id, this is how I would do it:

protected function _beforeSave(Varien_Object $customer)
{
    $customer = Mage::getModel('customer/customer');
    $customer->loadByEmail('CUSTOMER_EMAIL');

    if($customer->hasData()) {
        //email already exists
    }

    //email does not exists
    parent::_beforeSave($customer);
}
2
  • Thank you , so the new code would be the following?
    – Max Smith
    Jun 8, 2015 at 10:41
  • No, its a little mix of concepts, I'll update my answer.
    – lloiacono
    Jun 8, 2015 at 11:24
0
protected function _beforeSave(Varien_Object $customer)
{
parent::_beforeSave($customer);

if (!$customer->getEmail()) {
throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
}

 $adapter = $this->_getWriteAdapter();
 $bind    = array('email' => $customer->getEmail());

 $select = $adapter->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
->where('email = :email');
if ($customer->getSharingConfig()->isWebsiteScope()) {
$bind['website_id'] = (int)$customer->getWebsiteId();
$customer = Mage::getModel('customer/customer');
$customer->loadByEmail($login['email']);
}
if ($customer->getId()) {
$bind['entity_id'] = (int)$customer->getId();
$select->where('entity_id != :entity_id');
}

$result = $adapter->fetchOne($select, $bind);
if($customer->hasData()){
throw Mage::exception(
    'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
    Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
);
}

// set confirmation key logic
if ($customer->getForceConfirmed()) {
$customer->setConfirmation(null);
} elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
$customer->setConfirmation($customer->getRandomConfirmationKey());
}
// remove customer confirmation key from database, if empty
if (!$customer->getConfirmation()) {
$customer->setConfirmation(null);
}

return $this;
}
1
  • Would this be correct?
    – Max Smith
    Jun 8, 2015 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.