Created
February 22, 2022 04:23
-
-
Save tuanhaviet22/ef7ad1f5b3ad7b5ee032fb99ba6c1f70 to your computer and use it in GitHub Desktop.
Export Customer in Magento 1
This file contains 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 | |
/** | |
* Sutunam | |
* | |
* HOW IT WORK | |
* | |
* This script use for export customer data with custom column | |
* and custom logic | |
* | |
* HOW TO RUN | |
* | |
* This script was executed by CLI php $path-to-script | |
* Sample: php shell/sutunam/exportCustomer.php | |
* | |
* The exported file was place at folder var/export/stn_customers.csv | |
* | |
*/ | |
require_once __DIR__ . '/../abstract.php'; | |
ini_set('memory_limit', '-1'); | |
class Mage_Shell_Sutunam_ExportCustomer extends Mage_Shell_Abstract | |
{ | |
protected $_time; | |
protected $_file; | |
protected $_currentPage = 1; | |
protected $_pageSize = 500; | |
protected $_count = 1; | |
/** | |
* "Prénom " (first name) | |
* "Nom" (last name) | |
* "Email" (email) | |
* "Téléphone" (phone number) | |
* "Adresse" (address) | |
* "Code postal" (zip code) | |
* "Ville" (City) | |
* "Pays" (Country) | |
* "Date de naissance" (date of birth) | |
* "Genre" (genre) | |
* "Age" (age) | |
* "Date du dernier achat" (date of last purchase ) | |
* | |
* @var string[] | |
*/ | |
protected $_header = [ | |
"Prénom", | |
"Nom", | |
"Email", | |
"Téléphone", | |
"Adresse", | |
"Code postal", | |
"Ville", | |
"Pays", | |
"Date de naissance", | |
"Genre", | |
"Age", | |
"Date du dernier achat" | |
]; | |
/** | |
* @return Mage_Shell_Sutunam_ExportCustomer|void | |
*/ | |
public function _construct() | |
{ | |
$path = Mage::getBaseDir('export'); | |
$this->_file = fopen($path . '/stn_customers.csv', 'w'); | |
fputcsv($this->_file, $this->_header); | |
$this->_time = microtime(true); | |
parent::_construct(); | |
} | |
public function __destruct() | |
{ | |
echo Mage::helper('core')->__('Script executed in %s s', microtime(true) - $this->_time) . PHP_EOL; | |
} | |
/** | |
* Execute export script | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
$customerCollection = Mage::getModel('customer/customer') | |
->getCollection(); | |
$total = $customerCollection->getSize(); | |
$customerCollection->setPageSize($this->_pageSize); | |
$pages = $customerCollection->getLastPageNumber(); | |
do { | |
$customerCollection->setCurPage($this->_currentPage); | |
Mage::getSingleton('core/resource_iterator')->walk($customerCollection->load()->getSelect(), | |
[[$this, 'callback']]); | |
$this->_currentPage++; | |
$customerCollection->clear(); | |
} while ($this->_currentPage <= $pages); | |
echo "Exported record: $this->_count/$total \n"; | |
fclose($this->_file); | |
} | |
/** | |
* Callback function for walk collection | |
* Should be use walk collection for big data | |
* See more at: https://inchoo.net/magento/working-with-large-magento-collections/ | |
* | |
* @param $args | |
* | |
* @return void | |
*/ | |
public function callback($args) | |
{ | |
$data = $args['row']; | |
$customerId = $data['entity_id']; | |
$gender = $age = $dob = $dobts = $telephone = $countryName = $latestPurchaseDate = $address = $city = $postalCode = null; | |
/* @var $customerModel Mage_Customer_Model_Customer */ | |
$customerModel = Mage::getModel('customer/customer') | |
->load($customerId); | |
$billingAddress = $customerModel->getDefaultBillingAddress(); | |
$shippingAddress = $customerModel->getDefaultShippingAddress(); | |
if ($customerModel->getGender() == 1) { | |
$gender = "Male"; | |
} elseif ($customerModel->getGender() == 2) { | |
$gender = "Female"; | |
} | |
if ($dob = $customerModel->getDob()) { | |
$dobts = strtotime($dob); | |
$dob = date("Y/m/d", $dobts); | |
$age = date('Y') - date('Y', $dobts); | |
} | |
if ($shippingAddress) { | |
$countryModel = Mage::getModel('directory/country')->loadByCode($shippingAddress->getCountry()); | |
$countryName = $countryModel->getName(); | |
$telephone = trim($shippingAddress->getData('telephone')); | |
// Get latest order of customer | |
$orderCollection = Mage::getModel('sales/order')->getCollection() | |
->addFilter('customer_id', $customerId) | |
->setOrder('created_at', 'desc')->getFirstItem(); | |
$latestPurchaseDate = $orderCollection->getCreatedAt(); | |
$address = $shippingAddress->getData('street'); | |
$postalCode = $billingAddress->getPostcode(); | |
$city = $shippingAddress->getData('city'); | |
} | |
$row = [ | |
$customerModel->getFirstname(), | |
$customerModel->getLastname(), | |
$customerModel->getEmail(), | |
$telephone, | |
$address, | |
$postalCode, | |
$city, | |
$countryName, | |
$dob, | |
$gender, | |
$age, | |
$latestPurchaseDate | |
]; | |
fputcsv($this->_file, $row); | |
$this->_count++; | |
} | |
} | |
$shell = new Mage_Shell_Sutunam_ExportCustomer(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment