1

Magento 1.9.x trying to save $customer data => "Uncaught Mage_Customer_Exception: This customer email already exists"

We are looping thru all customers, updating data and saving

Only when saving a customer with an email that already exists: we see the error Uncaught Mage_Customer_Exception: This customer email already exists

Although when I check Magento the email does exist twice, but in different stores. So it should! save the customer, nothing is wrong

Question: how can we prevent this from happening?

We are looping data as follows

$customers = mage::getModel('customer/customer')->getCollection()
    ->addNameToSelect();

$cnt_me = 0;
foreach ($customers as $customer) {
    $data = array();
    $customer_id = $customer->getId();
    $customer_name = $customer->getName();
    $customer_email = $customer->getEmail();
    $orders = Mage::getModel('sales/order')->getCollection()
        ->addFieldToFilter('customer_id', $customer_id)
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()));
    foreach ($orders as $order) {
        foreach ($order->getAllItems() as $item) 

{

then later we do the following

if ($save) {
    $this->_dataSaveAllowed = true;
    $customer->save();
} else {
    continue;
}
snh_nl
  • 5,442
  • 13
  • 69
  • 133

2 Answers2

0

You need to do following things.

$store_Id = ''; // PASS STORE ID FOR WHICH YOU GET COLLECTION

$store = Mage::getModel('core/store')->load($store_Id);

$customers = Mage::getModel('customer/customer')->getCollection()
           ->addFieldToFilter('store_id',$store_Id)
           ->addNameToSelect();

foreach ($customers as $customer) 
{
   .........................
   $customer->setStore($store);
   $customer->save();
}
Dhiren Vasoya
  • 9,484
  • 12
  • 33
  • 59
0

The solution is to : double (yes double) check if there is max 1 account per email per store.

In our case somehow customers were created with store is admin, but storeview is the same as a store where the user already existed.

So the solution was to clean the data and make absolutely certain there are no duplicate emails

snh_nl
  • 5,442
  • 13
  • 69
  • 133