Created
January 6, 2015 15:38
-
-
Save epson121/140ad80d2e3309b5eb3e to your computer and use it in GitHub Desktop.
xcart_to_magento_customers.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once 'app/Mage.php'; | |
| error_reporting(E_ALL | E_STRICT); | |
| ini_set('display_errors', 1); | |
| set_time_limit(0); | |
| Mage::app('admin')->setUseSessionInUrl(false); | |
| $filePath = 'xcart_customers.csv'; | |
| $defaultStore = Mage::app()->getDefaultStoreView(); | |
| $defaultStoreId = $defaultStore->getId(); | |
| $adminStoreId = '0'; | |
| $defaultNotAnySymbol = 'N/A'; | |
| $coreResource = Mage::getSingleton('core/resource'); | |
| $writeConnection = $coreResource->getConnection('core_write'); | |
| $readConnection = $coreResource->getConnection('core_read'); | |
| $nextCustomerEntityId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer/entity')); | |
| $nextCustomerEntityIntId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_entity_int')); | |
| $nextCustomerEntityTextId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_entity_text')); | |
| $nextCustomerEntityVarcharId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_entity_varchar')); | |
| $nextCustomerEntityDatetimeId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_entity_datetime')); | |
| $nextCustomerAddressEntityId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer/address_entity')); | |
| $nextCustomerAddressEntityIntId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_address_entity_int')); | |
| $nextCustomerAddressEntityTextId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_address_entity_text')); | |
| $nextCustomerAddressEntityVarcharId = Mage::getResourceHelper('importexport')->getNextAutoincrement($coreResource->getTableName('customer_address_entity_varchar')); | |
| $customerEntity = array(); | |
| $customerEntityInt = array(); | |
| $customerEntityVarchar = array(); | |
| $customerEntityText = array(); | |
| $customerEntityDatetime = array(); | |
| $customerAddressEntity = array(); | |
| $customerAddressEntityInt = array(); | |
| $customerAddressEntityText = array(); | |
| $customerAddressEntityVarchar = array(); | |
| $defaultCustomerEntity = array( | |
| 'entity_type_id' => 1, | |
| 'attribute_set_id' => 0, | |
| 'website_id' => 1, | |
| 'group_id' => 1, | |
| 'increment_id' => NULL, | |
| 'store_id' => 1, | |
| 'is_active' => 1, | |
| 'disable_auto_group_change' => 0, | |
| ); | |
| // entity_id, email, created_at, updated_at | |
| $defaultCustomerEntityInt = array( | |
| 'entity_type_id' => 1 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $defaultCustomerEntityVarchar = array( | |
| 'entity_type_id' => 1 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $defaultCustomerEntityDatetime = array( | |
| 'entity_type_id' => 1 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $defaultCustomerAddressEntity = array( | |
| 'entity_type_id' => 2, | |
| 'attribute_set_id' => 0, | |
| 'increment_id' => NULL, | |
| 'is_active' => 1, | |
| ); | |
| // entity_id, parent_id, created_at, updated_at | |
| $defaultCustomerAddressEntityInt = array( | |
| 'entity_type_id' => 2 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $defaultCustomerAddressEntityText = array( | |
| 'entity_type_id' => 2 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $defaultCustomerAddressEntityVarchar = array( | |
| 'entity_type_id' => 2 | |
| ); | |
| // value_id, attribute_id, entity_id, value | |
| $csvAdapter = Mage_ImportExport_Model_Import_Adapter::factory('csv', $filePath); | |
| //echo iterator_count($csvAdapter); die(); | |
| $emails = array(); | |
| $row = 1; | |
| if (($handle = fopen("emails.csv", "r")) !== FALSE) { | |
| while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { | |
| $emails[] = $data[0]; | |
| } | |
| fclose($handle); | |
| } | |
| $customer = array(); | |
| foreach($csvAdapter as $row) { | |
| if ($counter % 1000 == 0) | |
| echo $counter . "\n"; | |
| $counter++; | |
| if (!$row['email']) | |
| continue; | |
| $row['email'] = strtolower($row['email']); | |
| if (in_array($row['email'], $emails)) | |
| continue; | |
| $emails[] = $row['email']; | |
| // HELPER ARRAY | |
| $customer[$nextCustomerEntityId] = array(); | |
| // CUSTOMER ENTITY | |
| $customerEntity[] = array_merge($defaultCustomerEntity, array( | |
| 'entity_id' => $nextCustomerEntityId, | |
| 'email' => $row['email'], | |
| 'created_at' => convertTime($row['first_login']), | |
| 'updated_at' => convertTime($row['first_login']) | |
| )); | |
| // if exists data for address, create one | |
| if ($row['b_address'] && getRegionCode($row['b_state'], $row['b_country']) | |
| && $row['firstname'] && $row['lastname'] && $row['b_city'] && $row['b_zipcode']) { | |
| // CUSTOMER BILLING ADDRESS ENTITY | |
| $customerAddressEntity[] = array_merge($defaultCustomerAddressEntity, array( | |
| 'entity_id' => $nextCustomerAddressEntityId, | |
| 'parent_id' => $nextCustomerEntityId, | |
| 'created_at' => convertTime($row['first_login']), | |
| 'updated_at' => convertTime($row['first_login']) | |
| )); | |
| $customer[$nextCustomerEntityId]['billing_address'] = $nextCustomerAddressEntityId; | |
| $customer[$nextCustomerEntityId]['shipping_address'] = $nextCustomerAddressEntityId; | |
| $customerAddressEntityText[] = getEavRow( | |
| $defaultCustomerAddressEntityText, | |
| $nextCustomerAddressEntityTextId, | |
| 25, | |
| $nextCustomerAddressEntityId, | |
| $row['b_address']); | |
| $nextCustomerAddressEntityTextId++; | |
| if (getRegionCode($row['b_state'], $row['b_country'])) { | |
| $customerAddressEntityInt[] = getEavRow( | |
| $defaultCustomerAddressEntityInt, | |
| $nextCustomerAddressEntityIntId, | |
| 29, | |
| $nextCustomerAddressEntityId, | |
| getRegionCode($row['b_state'], $row['b_country']) | |
| ); | |
| $nextCustomerAddressEntityIntId++; | |
| } | |
| if ($row['b_city']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 26, | |
| $nextCustomerAddressEntityId, | |
| $row['b_city']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['company']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 24, | |
| $nextCustomerAddressEntityId, | |
| $row['company']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['b_country']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 27, | |
| $nextCustomerAddressEntityId, | |
| $row['b_country']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } else { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 27, | |
| $nextCustomerAddressEntityId, | |
| 'US'); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['fax']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 32, | |
| $nextCustomerAddressEntityId, | |
| $row['fax']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['firstname']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 20, | |
| $nextCustomerAddressEntityId, | |
| $row['firstname']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['lastname']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 22, | |
| $nextCustomerAddressEntityId, | |
| $row['lastname']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['b_zipcode']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 30, | |
| $nextCustomerAddressEntityId, | |
| $row['b_zipcode']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['b_state']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 28, | |
| $nextCustomerAddressEntityId, | |
| $row['b_state']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| if ($row['phone']) { | |
| $customerAddressEntityVarchar[] = getEavRow( | |
| $defaultCustomerAddressEntityVarchar, | |
| $nextCustomerAddressEntityVarcharId, | |
| 31, | |
| $nextCustomerAddressEntityId, | |
| $row['phone']); | |
| $nextCustomerAddressEntityVarcharId++; | |
| } | |
| $nextCustomerAddressEntityId++; | |
| } | |
| // if exists billing address, set as default | |
| if ($customer[$nextCustomerEntityId]['billing_address']) { | |
| // default billing | |
| $customerEntityInt[] = getEavRow( | |
| $defaultCustomerEntityInt, | |
| $nextCustomerEntityIntId, | |
| 13, | |
| $nextCustomerEntityId, | |
| $customer[$nextCustomerEntityId]['billing_address']); | |
| $nextCustomerEntityIntId++; | |
| } | |
| // if exists shipping address, set as default | |
| if ($customer[$nextCustomerEntityId]['shipping_address']) { | |
| // default shipping | |
| $customerEntityInt[] = getEavRow( | |
| $defaultCustomerEntityInt, | |
| $nextCustomerEntityIntId, | |
| 14, | |
| $nextCustomerEntityId, | |
| $customer[$nextCustomerEntityId]['shipping_address']); | |
| $nextCustomerEntityIntId++; | |
| } | |
| // firstname | |
| if ($row['firstname']) { | |
| $customerEntityVarchar[] = getEavRow( | |
| $defaultCustomerEntityVarchar, | |
| $nextCustomerEntityVarcharId, | |
| 5, | |
| $nextCustomerEntityId, | |
| escapeCharacters($row['firstname']) | |
| ); | |
| $nextCustomerEntityVarcharId++; | |
| } | |
| // lastname | |
| if ($row['firstname']) { | |
| $customerEntityVarchar[] = getEavRow( | |
| $defaultCustomerEntityVarchar, | |
| $nextCustomerEntityVarcharId, | |
| 7, | |
| $nextCustomerEntityId, | |
| escapeCharacters($row['lastname']) | |
| ); | |
| $nextCustomerEntityVarcharId++; | |
| } | |
| // password_hash | |
| $customerEntityVarchar[] = getEavRow( | |
| $defaultCustomerEntityVarchar, | |
| $nextCustomerEntityVarcharId, | |
| 12, | |
| $nextCustomerEntityId, | |
| hash('sha256', generatePassword()) | |
| ); | |
| $nextCustomerEntityVarcharId++; | |
| // created_in | |
| $customerEntityVarchar[] = getEavRow( | |
| $defaultCustomerEntityVarchar, | |
| $nextCustomerEntityVarcharId, | |
| 3, | |
| $nextCustomerEntityId, | |
| convertTime($row['first_login']) | |
| ); | |
| $nextCustomerEntityVarcharId++; | |
| $nextCustomerEntityId++; | |
| } | |
| // var_dump($customerEntity); | |
| // var_dump($customerEntityInt); | |
| // var_dump($customerAddressEntity); | |
| // var_dump($customerAddressEntityVarchar); | |
| // die(); | |
| $fp = fopen('emails.csv', 'w'); | |
| foreach ($emails as $fields) { | |
| fputcsv($fp, array($fields)); | |
| } | |
| fclose($fp); | |
| insertCustomers($writeConnection, $coreResource, $customerEntity, $customerEntityInt, $customerEntityVarchar, | |
| $customerAddressEntity, $customerAddressEntityText, $customerAddressEntityInt, $customerAddressEntityVarchar); | |
| function insertCustomers($connection, $resource, $customerEntity, $customerEntityInt, $customerEntityVarchar, | |
| $customerAddressEntity, $customerAddressEntityText, $customerAddressEntityInt, $customerAddressEntityVarchar) { | |
| $connection->raw_query("SET foreign_key_checks = 0;"); | |
| $connection->insertMultiple($resource->getTableName('customer/entity'), $customerEntity); | |
| echo "Customer Entity OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer_entity_int'), $customerEntityInt); | |
| echo "Customer Int OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer_entity_varchar'), $customerEntityVarchar); | |
| echo "Customer Varchar OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer/address_entity'), $customerAddressEntity); | |
| echo "Customer Address OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer_address_entity_text'), $customerAddressEntityText); | |
| echo "Customer Address Text OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer_address_entity_int'), $customerAddressEntityInt); | |
| echo "Customer Address Int OK" . "\n"; | |
| $connection->insertMultiple($resource->getTableName('customer_address_entity_varchar'), $customerAddressEntityVarchar); | |
| echo "Customer Address Varchar OK" . "\n"; | |
| $connection->raw_query("SET foreign_key_checks = 1;"); | |
| } | |
| function escapeCharacters($string) { | |
| return trim(preg_replace('/(\r\n|\r|\n)+/', ' ',$string)); | |
| } | |
| function convertTime($epoch) { | |
| if ($epoch) | |
| return (new DateTime("@$epoch"))->format('Y-m-d H:i:s'); // convert UNIX timestamp to PHP DateTime | |
| return convertTime(time()); | |
| } | |
| function getEavRow($default, $valueId, $attributeId, $entityId, $value) { | |
| return array_merge($default, array( | |
| 'value_id' => $valueId, | |
| // city | |
| 'attribute_id' => $attributeId, | |
| 'entity_id' => $entityId, | |
| 'value' => escapeCharacters($value) | |
| )); | |
| } | |
| function generatePassword() { | |
| $chars = Mage_Core_Helper_Data::CHARS_PASSWORD_LOWERS | |
| . Mage_Core_Helper_Data::CHARS_PASSWORD_UPPERS | |
| . Mage_Core_Helper_Data::CHARS_PASSWORD_DIGITS | |
| . Mage_Core_Helper_Data::CHARS_PASSWORD_SPECIALS; | |
| return Mage::helper('core')->getRandomString(8, $chars); | |
| } | |
| $codes = array(); | |
| function getRegionCode($state, $country) { | |
| if ($codes[$state]) { | |
| return $codes[$state]; | |
| } | |
| $regionModel = Mage::getModel('directory/region')->loadByCode($state, $country); | |
| $regionId = $regionModel->getId(); | |
| $codes[$state] = $regionId; | |
| return $regionId; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment