The database is a well-organized collection of information that can be easily accessed, managed and updated, and this data is stored in rows, columns, and tables, and with its own index that makes it easier to fetch relevant information whenever needed. One powerful eCommerce CMS Magento relies on MYSQL Database which is completely open source and one of the most popular languages for adding, accessing and managing content in a database in an easy and flexible way. And your customer’s information plays a vital role in your business because without having your customer’s information your business is almost incapable of doing marketing and suggest products to your customers.
Sometimes it happens that the store admin wants fresh Magento 2 update without importing all the data back or admin came across a requirement of just transferring Magento 1 store customers to Magento 2 store which is a cumbersome task if you are the newbie. So, we are back with another powerful script that will help you to transfer all your customer information to your Magento 2 stores including store credentials, address and other information.
To do the same first we need to create ‘customer.php’ in Magento 1.x root folder to generate a main customer CSV file that we will later import to Magento 2 store.
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 60 61 62 63 64 65 66 67 68 69 |
<!--?php try { include_once('app/Mage.php'); Mage::app('admin'); $collection = mage::getModel('customer/customer')->getCollection(); $fp = fopen('customer.csv', 'w+'); $csvHeader = getHeadRowValues(); fputcsv($fp, $csvHeader,","); foreach($collection as $customer1){ $customer = Mage::getModel('customer/customer')->load($customer1->getId()); $record = array( $customer->getEmail(), "base", "default", "", $customer->getCreatedAt(), $customer->getCreatedIn(), $customer->getDisableAutoGroupChange(), $customer->getDob(), $customer->getFirstname(), "", "1", $customer->getLastname(), $customer->getMiddlename(), $customer->getPasswordHash(), $customer->getPrefix(), "", "", $customer->getStoreId(), $customer->getSuffix(), $customer->getTaxvat(), $customer->getUpdatedAt(), $customer->getWebsiteId(), "" ); fputcsv($fp, $record, ","); } fclose($fp); }catch (Exception $e){ echo $e->getMessage(); } function getHeadRowValues() { return array( "email", "_website", "_store", "confirmation", "created_at", "created_in", "disable_auto_group_change", "dob", "firstname", "gender", "group_id", "lastname", "middlename", "password_hash", "prefix", "rp_token", "rp_token_created_at", "store_id", "suffix", "taxvat", "updated_at", "website_id", "password" ); } ?--> |
After that, we need to create one more ‘customeraddress.php’ script in the same location of Magento 1.x root folder for creating all customer address CSV file.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<!--?php try { include_once('app/Mage.php'); Mage::app('admin'); $collection = mage::getModel('customer/customer')->getCollection(); $fp = fopen('customerAddress.csv', 'w+'); $csvHeader = getHeadRowValues(); fputcsv($fp, $csvHeader,","); foreach($collection as $customer1){ $customer = Mage::getModel('customer/customer')->load($customer1->getId()); $defaultBilling = $customer->getDefaultBillingAddress(); $defaultShipping = $customer->getDefaultShippingAddress(); foreach ($customer1->getAddresses() as $address) { $isDbilling = ""; $isDShipping = ""; if($defaultBilling->getID() == $address->getEntityId()) { $isDbilling = "1"; } if($defaultShipping->getID() == $address->getEntityId()) { $isDShipping = "1"; } $street = $address->getStreet(); $strStreet = implode(" ",$street); $regin = ""; if($address->getRegion()) $regin = $address->getRegion(); $record = array( "base", $customer->getEmail(), $address->getEntityId(), $address->getCity(), $address->getCompany(), $address->getCountryId(), $address->getFax(), $address->getFirstname(), $address->getLastname(), $address->getMiddlename(), $address->getPostcode(), $address->getPrefix(), $regin, $address->getRegionId(), $strStreet, $address->getSuffix(), $address->getTelephone(), $address->getVatId(), "", "", "", "", $isDbilling, $isDShipping ); fputcsv($fp, $record, ","); } } fclose($fp); } catch (Exception $e){ echo $e->getMessage(); } function getHeadRowValues() { return array( "_website", "_email", "_entity_id", "city", "company", "country_id", "fax", "firstname", "lastname", "middlename", "postcode", "prefix", "region", "region_id", "street", "suffix", "telephone", "vat_id", "vat_is_valid", "vat_request_date", "vat_request_id", "vat_request_success", "_address_default_billing_", "_address_default_shipping_" ); } ?--> |
After running both the script in your Magento 1.x root folder now you can find “customer.csv” file that is generated using the first script and “customerAddress.csv” file that is generated after successful execution of the second script.
Now it’s time to import these CSV files into your Magento 2 backend and for that purpose navigate to System->Import
For importing “customer.csv” file select “Customers Main File” option from the dropdown and for importing “customerAddress.csv” select Customer Addresses option from the dropdown and select remaining option according to your business need.
That’s it! If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.
And, Let us know if you are facing an issue while implementing this code.
Happy Transfer!
Exactly what I was looking for – thanks.
However, I have an issue with the GroupID information – this is set to ‘1’ for all customers. Anything I made wrong or it that not covered?
The code for the Group Id is there, but currently, we pass static there, but you can make it dynamic when you export the data according to your requirements.
After running both the script in Magento 1.x root folder I not receive customer.csv and customerAddress.csv
Kindly check your download folder of the system.
Hi,
Thanks for this solution. Think it’s exactly what I need but the second script gives me an error. First script customer.php runs great and gives me an nice csv file but when I run the second script customeraddress.php I get this error. I’ve copy paste it several times and run it.
php -f /home/itennis/domains/itennis.nl/public_html/customeraddress.php
PHP Fatal error: Uncaught Error: Call to a member function getID() on boolean in /home/xxxxx/domains/xxxxxxx.xx/public_html/customeraddress.php:29
Stack trace:
#0 {main}
thrown in /home/xxxxx/domains/xxxxxxx.xx/public_html/customeraddress.php on line 29
Could you please help me?
Kindly check there should be some customers whose default billing or shipping address are not set, possible reason of the error.
The same problem happened to me. How do you easily identify the customers whose default billing or shipping are not set?
Kindly check for which customer this script gives you an error, actually that customer might have some issues like some entry deleted from the database or something like that