Skip to content

Instantly share code, notes, and snippets.

@jclecas
Created March 27, 2018 15:46
Show Gist options
  • Save jclecas/260150e985201f3903eacee01757d1e5 to your computer and use it in GitHub Desktop.
Save jclecas/260150e985201f3903eacee01757d1e5 to your computer and use it in GitHub Desktop.
Magento 1 Export customers data and customers address
<?php
require_once 'log_tools.php';
/**
* Customers Export Data
*/
class Caudalie_Shell_Customers_Export_Data extends Caudalie_Shell_Log_Tools
{
protected $totalPages = null;
/** @var $collection Mage_Customer_Model_Resource_Customer_Collection */
protected $collection = null;
protected $from_id = false;
protected $limit;
/**
* Put here your store_ids if you want to filter on it !
* @var array
*/
protected $stores = array();
protected $contactFields = array(
'entity_id' => '',
'website_id' => '',
'store_id' => '',
'crm_id' => '',
'email' => '',
'password_hash' => '',
'group_id' => '',
'prefix' => '',
'firstname' => '',
'lastname' => '',
'dob' => '',
'created_at' => '',
'billing_prefix' => '',
'billing_firstname' => '',
'billing_lastname' => '',
'billing_street' => '',
'billing_city' => '',
'billing_region_id' => '',
'billing_region' => '',
'billing_postcode' => '',
'billing_country_id' => '',
'billing_telephone' => '',
'shipping_prefix' => '',
'shipping_firstname' => '',
'shipping_lastname' => '',
'shipping_street' => '',
'shipping_city' => '',
'shipping_region_id' => '',
'shipping_region' => '',
'shipping_postcode' => '',
'shipping_country_id' => '',
'shipping_telephone' => ''
);
/**
* Get Customer Collection
*/
public function initCustomerCollection()
{
$this->collection = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('*');
// Partial export ?
if ($this->from_id) {
$this->collection->addAttributeToFilter('entity_id', array('gt' => $this->from_id));
}
// Filter on store_ids ?
if (is_array($this->stores) && count($this->stores) > 0) {
$this->collection->addAttributeToFilter('store_id', array('in' => $this->stores));
}
$this->collection->setPageSize(100);
$this->totalPages = $this->collection->getLastPageNumber();
}
/**
* Clean street
*
* @param $street
* @return mixed
*/
public function cleanStreet($street)
{
return str_replace(array("\r", "\n"), array('', '|*|'), $street);
}
public function exportCustomers()
{
ini_set('memory_limit','500M');
$customerCount = 0;
try {
// Customer collection
$this->initCustomerCollection();
// Filename
$exportFilePath = Mage::getBaseDir('var') . DS . 'export';
$filename = date('Ymd_Hi') . '_export_customers.csv';
$io = new Varien_Io_File();
if (!$io->checkAndCreateFolder($exportFilePath)) {
throw new Exception('Export path not valid');
}
// Open file
$handle = fopen($exportFilePath . DS . $filename, 'w+');
fputcsv($handle, array_keys($this->contactFields));
// Export customers
for ($currentPage = 1; $currentPage <= $this->totalPages; $currentPage++) {
$this->collection->setCurPage($currentPage);
$this->collection->load();
/** @var Mage_Customer_Model_Customer $customer */
foreach ($this->collection as $customer) {
// Init Customer array
$data = $this->contactFields;
// Customer data
$data['entity_id'] = $customer->getData('entity_id');
$data['website_id'] = $customer->getData('website_id');
$data['store_id'] = $customer->getData('store_id');
$data['crm_id'] = $customer->getData('crm_id');
$data['email'] = $customer->getData('email');
$data['password_hash'] = $customer->getData('password_hash');
$data['group_id'] = $customer->getData('group_id');
$data['prefix'] = $customer->getData('prefix');
$data['firstname'] = $customer->getData('firstname');
$data['lastname'] = $customer->getData('lastname');
$data['dob'] = $customer->getData('dob');
$data['created_at'] = $customer->getData('created_at');
// Default Billing ?
$billingAddress = $customer->getDefaultBillingAddress();
if ($billingAddress) {
$data['billing_prefix'] = $billingAddress->getData('prefix');
$data['billing_firstname'] = $billingAddress->getData('firstname');
$data['billing_lastname'] = $billingAddress->getData('lastname');
$data['billing_street'] = $this->cleanStreet($billingAddress->getData('street'));
$data['billing_city'] = $billingAddress->getData('city');
$data['billing_region_id'] = $billingAddress->getData('region_id');
$data['billing_region'] = $billingAddress->getData('region');
$data['billing_postcode'] = $billingAddress->getData('postcode');
$data['billing_country_id'] = $billingAddress->getData('country_id');
$data['billing_telephone'] = $billingAddress->getData('telephone');
}
$billingAddress = null;
// Default Shipping ?
$shippingAddress = $customer->getDefaultShippingAddress();
if ($shippingAddress) {
$data['shipping_prefix'] = $shippingAddress->getData('prefix');
$data['shipping_firstname'] = $shippingAddress->getData('firstname');
$data['shipping_lastname'] = $shippingAddress->getData('lastname');
$data['shipping_street'] = $this->cleanStreet($shippingAddress->getData('street'));
$data['shipping_city'] = $shippingAddress->getData('city');
$data['shipping_region_id'] = $shippingAddress->getData('region_id');
$data['shipping_region'] = $shippingAddress->getData('region');
$data['shipping_postcode'] = $shippingAddress->getData('postcode');
$data['shipping_country_id'] = $shippingAddress->getData('country_id');
$data['shipping_telephone'] = $shippingAddress->getData('telephone');
}
$shippingAddress = null;
fputcsv($handle, $data);
$data = null;
$customerCount++;
}
$this->collection->clear();
echo "Finished page $currentPage of {$this->totalPages} \n";
if ($this->limit) {
if ($currentPage == 10) {
break;
}
}
}
fclose($handle);
} catch (Exception $e) {
//$response['error'] = $e->getMessage();
Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
echo "less var/export/$filename \n";
}
/**
* Run script
*
*/
public function run()
{
if (!$this->getArg('export')) {
echo $this->usageHelp();
return;
}
$this->limit = true;
if ($this->getArg('export') === 'nolimit') {
$this->limit = false;
}
$this->from_id = false;
if ($this->getArg('from_id')) {
$this->from_id = $this->getArg('from_id');
}
$this->exportCustomers();
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage(s):
php shell/customers_export_data.php --export
php shell/customers_export_data.php --export nolimit
php shell/customers_export_data.php --export nolimit --from_id 123456
Option(s):
--export Export all customers with a limit of 10 pages
--export nolimit Export all customers without limit
--from_id 123456 Partial export of customers from a customer_id given
USAGE;
}
}
$shell = new Caudalie_Shell_Customers_Export_Data();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment