4

I imported orders into Magento using some migration tools. When a return customer tries to place an order, Magento prevents them from doing so and says "This customer email already exists." This is despite the fact that they are already logged into Magento.

Did I import/migrate into the Magento database incorrectly? Or could something else be causing this?

Any suggestions are very much appreciated.

Isaac Y
  • 660
  • 1
  • 9
  • 27

1 Answers1

9

The exception you get is generated by the customer resource model's _beforeSave function, which checks if a customer with the given email address exists. The check's code:

    $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
        );
    }

You customers are logged in, which means the condition $customer->getId() is true. But since you get an exception, I suggest, there are duplicate customer accounts with the same emails.

Can it be that your import tool has created duplicates in the customer data? That is the only reason I can think of. Check your database with this query:

 select email, count(*) from customer_entity group by email having count(*) > 1
Keyur Shah
  • 11,043
  • 4
  • 29
  • 48
Oleg Ishenko
  • 2,243
  • 1
  • 15
  • 16
  • Very very helpful. Thanks! I found that there are two emails that have duplicate accounts. One had accounts on separate stores (which I think is okay). The other was my own email. This is out of several thousand accounts. It worries me though, how could a duplicate been created in the first place? – Isaac Y Feb 20 '13 at 17:41
  • I did, but it was a while after I made this post..so I forgot to post the solution. I can't quite remember how it was resolved in the end, but it may have been one of these options: - I may have forgotten to set the order count in my new store, to what i left off in my old store - I was running multi-store setup and may have imported the customers to the wrong store, or had a mismatch between customers and/or orders. I'm not entirely sure though, and am sorry I cannot be more helpful. – Isaac Y Mar 10 '14 at 22:40
  • What is the cause of these duplicates? Seeing some (small percentage) in my own customer_entity table but with the same store_id! – drj201 Aug 15 '14 at 08:30
  • I have the same issue but my customers are in different group and different website then why its happening? – Prassanna D Manikandan Dec 11 '15 at 13:53
  • isn't this a bug in magento ??? because the select statement searching for duplicates should differenciate between stores and websites. – Mohamed23gharbi May 02 '16 at 16:17
  • is it possible to avoid duplicate accounts if you use just magento api (soap, xmlrpc, etc)? dups are possible if you update something directly in database, am i right? – Eugene Lebedev Mar 28 '17 at 18:10