Data is the heart of any business that keeps business alive. It can be anything related to employees, customers, products and competitors that is collected from different sources and gathered in one place so it helps to make the business succeed. But several times, it happens that you may lose your useful information because of technical issue, system error, database corrupt or it can be anything.
Losing such important data can break your business too. As this kind of information is useful for taxation, promotions and other business processes. At that time, recovering this deleted information become so important when it comes to your customers or orders related information. Many times, the Magento store owner has come across this kind of situation, so we are back with a useful article which will help you to recover your deleted customer information from your order details.
Simply make one “Script.php” file in your Magento root folder and run following script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<!--?php try { include_once('app/Mage.php'); Mage::app('admin'); $order = Mage::getModel('sales/order')->getCollection(); $notExist = 0; foreach($order as $item){ $email=""; $resource = Mage::getSingleton('core/resource'); $readConnection = $resource->getConnection('core_read'); $table = $resource->getTableName('sales_order_varchar'); $query = "SELECT value FROM {$table} WHERE entity_id =".$item->getEntityId()." AND attribute_id = 197"; //197 attribute id for email here we get email from order. $rows = $readConnection->fetchAll($query); if(@$rows[0]['value']){ $email = $rows[0]['value']; } else{ continue; } $billingAddress = $item->getBillingAddress(); $websiteId = Mage::app()->getWebsite()->getId(); $customerExist = Mage::getModel('customer/customer') ->getCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('email', $email); if($customerExist->getData()){ $customerData = $customerExist->getData(); $customerId = $customerData[0]['entity_id']; $item->setCustomerId($customerId); $item->save(); } else{ // Create Customer and assign to order $websiteId = Mage::app()->getWebsite()->getId(); $store = Mage::app()->getStore(); $firstname = $billingAddress['firstname']; $lastname = $billingAddress['lastname']; $email = $email; $password = "test123"; // Here you can set any password. $customer = Mage::getModel("customer/customer"); $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($firstname) ->setLastname($lastname) ->setEmail($email) ->setPassword($password); $customer->save(); $customerId = $customer->getId(); $item->setCustomerId($customerId); $item->save(); $notExist++; } } echo $notExist." Customer Restore Successfully"; } catch (Exception $e){ echo $e->getMessage(); } ?--> |
That’s it you are done! You have successfully recovered your deleted Magento Customers in less than a minute. Also, this script will automatically skip the customer creation process if the customer has more than one orders in the backend.
If this blog helps you share it with your colleagues and Magento Friends.
Let us know if you are facing an issue while implementing using this code by commenting below.
Happy Recovering!
Does this work with Magento v 2.4 ?
Does this work with Magento 2.4 ?
Having just got of the phone to a client that has “Select All” “Delete” rather than “Select Visible” “Delete”… I think this may well save the day! Thanks for sharing… Just one question… Have you given any thought to setting “Customer Since” date to “Order Date”?