Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php
  2. require_once('app/Mage.php');
  3. umask(0);
  4. if (!Mage::isInstalled()) {
  5. echo "Application is not installed yet, please complete install wizard first.";
  6. exit;
  7. }
  8. // Only for urls // Don't remove this
  9. $_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
  10. $_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
  11. Mage::app('admin')->setUseSessionInUrl(false);
  12. Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL);
  13. try {
  14. Mage::getConfig()->init();
  15. Mage::app();
  16. } catch (Exception $e) {
  17. Mage::printException($e);
  18. }
  19. ini_set('memory_limit','500M');
  20. $customerCount = 0;
  21. try{
  22. //configure the collection filters.
  23. $collection = Mage::getResourceModel('customer/customer_collection')
  24. ->addAttributeToSelect('entity_id')
  25. ->addAttributeToSelect('firstname');
  26.  
  27. //Add a page size to the result set.
  28. $collection->setPageSize(50);
  29. //discover how many page the result will be.
  30. $pages = $collection->getLastPageNumber();
  31. $currentPage = 1;
  32. //This is the file to append the output to.
  33. $fp = fopen('customers.csv', 'w');
  34. $addedKeys = false;
  35. do{
  36. //Tell the collection which page to load.
  37. $collection->setCurPage($currentPage);
  38. $collection->load();
  39. foreach ($collection as $customer){
  40. //write the collection array as a CSV.
  41. $customerArray = $customer->toArray();
  42. $customerREquiredArray['customer ID'] = $customerArray['entity_id'];
  43. $customerREquiredArray['First name'] = $customerArray['firstname'];
  44.  
  45. if($addedKeys == false){
  46. $keys = array_keys($customerREquiredArray);
  47. fputcsv($fp, $keys);
  48. $addedKeys = true;
  49. }
  50. //var_dump($customerArray); echo "nn";
  51. fputcsv($fp, $customerREquiredArray);
  52. //fwrite($fp,print_r($customerArray,true) . chr(10) );
  53. $customerCount++;
  54. }
  55. $currentPage++;
  56. //make the collection unload the data in memory so it will pick up the next page when load() is called.
  57. $collection->clear();
  58. //break; //DEBUG
  59. echo "Finished page $currentPage of $pages n";
  60. } while ($currentPage <= $pages);
  61. fclose($fp);
  62. } catch (Exception $e) {
  63. //$response['error'] = $e->getMessage();
  64. Mage::printException($e);
  65. }
  66. echo "Saved $customerCount customers to csv file n";
Add Comment
Please, Sign In to add comment