Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tegansnyder/4282003 to your computer and use it in GitHub Desktop.
Save tegansnyder/4282003 to your computer and use it in GitHub Desktop.
Produce a CSV of All Magento Customers with Recurring Profile Details
<?php
// Produce a CSV of All the Customers with Recurring Profile Details
error_reporting(E_ALL | E_STRICT);
require_once 'app/Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
$collection = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
$result = array();
$x = 0;
foreach ($collection as $customer) {
$customer_id = $customer->getId();
$group_id = $customer->getGroupId();
$email = $customer->getEmail();
$profile = Mage::getModel('sales/recurring_profile')->getCollection()
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $customer_id)
->getFirstItem();
$customerAddressId = $customer->getDefaultBilling();
$address = Mage::getModel('customer/address')->load($customerAddressId);
if ($profile->getId()) {
$result[$x]['customer_id'] = $customer_id;
$result[$x]['customer_name'] = $customer->getFirstname() . ' ' . $customer->getLastname();
$result[$x]['customer_email'] = $customer->getEmail();
$street = '';
$city = '';
$state = '';
$zip = '';
$country = '';
if ($customerAddressId){
$street = $address->getStreetFull();
$city = $address->getCity();
$state = $address->getRegion();
$zip = $address->getPostcode();
$country = $address->getCountryId();
}
$result[$x]['shipping_address'] = $street;
$result[$x]['shipping_city'] = $city;
$result[$x]['shipping_state'] = $state;
$result[$x]['shipping_zip'] = $zip;
$result[$x]['shipping_country'] = $country;
$result[$x]['email'] = $email;
$result[$x]['group_id'] = $group_id;
$result[$x]['has_profile'] = 'yes';
$result[$x]['profile_referenceId'] = $profile->getReferenceId();
} else {
$result[$x]['customer_id'] = $customer_id;
$result[$x]['customer_name'] = $customer->getFirstname() . ' ' . $customer->getLastname();
$result[$x]['customer_email'] = $customer->getEmail();
$street = '';
$city = '';
$state = '';
$zip = '';
$country = '';
if ($customerAddressId){
$street = $address->getStreetFull();
$city = $address->getCity();
$state = $address->getRegion();
$zip = $address->getPostcode();
$country = $address->getCountryId();
}
$result[$x]['shipping_address'] = $street;
$result[$x]['shipping_city'] = $city;
$result[$x]['shipping_state'] = $state;
$result[$x]['shipping_zip'] = $zip;
$result[$x]['shipping_country'] = $country;
$result[$x]['email'] = $email;
$result[$x]['group_id'] = $group_id;
$result[$x]['has_profile'] = 'no';
$result[$x]['profile_referenceId'] = '';
}
$x = $x + 1;
function outputCSV($data) {
$outputBuffer = fopen("php://output", 'w');
foreach($data as $val) {
fputcsv($outputBuffer, $val);
}
fclose($outputBuffer);
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=customers.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment