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;
}
-
Did you check website id? or your settings is global for all store?Sohel Rana– Sohel Rana2019年05月10日 18:53:39 +00:00Commented May 10, 2019 at 18:53
-
Update question with full code.Sohel Rana– Sohel Rana2019年05月10日 18:57:06 +00:00Commented May 10, 2019 at 18:57
2 Answers 2
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();
}
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