Fetch the Ordered Products through collection
I Have modified your code please use below code and you will get all customer orders in csv file.
<?php
require_once('app/Mage.php');
error_reporting(E_ALL);
ini_set('display_errors', 'On');
umask(0);
if (!Mage::isInstalled()) {
echo "Application is not installed yet, please complete install wizard first.";
exit;
}
// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL);
try {
Mage::getConfig()->init();
Mage::app();
} catch (Exception $e) {
Mage::printException($e);
}
ini_set('memory_limit','500M');
$customerCount = 0;
try{
//configure the collection filters.
$collection = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('entity_id')
->addAttributeToSelect('firstname')
->addAttributeToSelect('email');
//Add a page size to the result set.
$collection->setPageSize(50);
//discover how many page the result will be.
$pages = $collection->getLastPageNumber();
$currentPage = 1;
//This is the file to append the output to.
$fp = fopen('customers.csv', 'w');
$header=array("Customer Id","First name",'Email',"order id","order date","products","product create time");
fputcsv($fp, $header);
do{
//Tell the collection which page to load.
$collection->setCurPage($currentPage);
$collection->load();
foreach ($collection as $customer){
$customerArray = $customer->toArray();
$newordercollection = Mage::getModel("sales/order")->getCollection()->addFieldToFilter('customer_id',$customerArray['entity_id']);
$collection->setOrder('entity_id', 'ASC');
$ordervalue = $newordercollection->getFirstItem();
// get product information
$result=array();
$customerREquiredArray=array();
foreach ($ordervalue->getAllVisibleItems() as $item)
{
$createdAt = $ordervalue->getCreatedAt();
$myTimezone = new \DateTimeZone("Asia/Kolkata");
$UTC = new DateTimeZone("UTC");
$newDate = new \DateTime( $createdAt, $UTC );
$newDate->setTimezone( $myTimezone );
$createdAt = $newDate->format('Y-m-d H:i:s');
$productCreatedAt=$item->getProduct()->getCreatedAt();
$pNewdate = new \DateTime( $productCreatedAt, $UTC );
$pNewdate->setTimezone( $myTimezone );
$productCreatedAt = $pNewdate->format('Y-m-d H:i:s');
$sku = $item->getSku();
$eid = $ordervalue->getOrderId();
$order = Mage::getModel("sales/order")->load($eid);
$customerREquiredArray['customer ID'] = $customerArray['entity_id'];
$customerREquiredArray['First name'] = $customerArray['firstname'];
$customerREquiredArray['Email'] = $customerArray['email'];
$customerREquiredArray['order id']= $ordervalue->getIncrementId();
$customerREquiredArray['order date']= $createdAt;
$customerREquiredArray['products']= $sku;
$customerREquiredArray['product create time']= $productCreatedAt;
fputcsv($fp, $customerREquiredArray);
break;
}
}
$currentPage++;
$customerCount++;
$collection->clear();
echo "Finished page $currentPage of $pages \n";
} while ($currentPage <= $pages);
fclose($fp);
} catch (Exception $e) {
Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
Here is output
I have updated your problematic code check.
foreach ($ordervalue->getAllVisibleItems() as $item)
{
$sku[] = $item->getSku(); //set sku in array
}
$eid = $ordervalue->getOrderId();
$order = Mage::getModel("sales/order")->load($eid);
}
//write the collection array as a CSV.
$customerREquiredArray['customer ID'] = $customerArray['entity_id'];
$customerREquiredArray['First name'] = $customerArray['firstname'];
$customerREquiredArray['Email'] = $customerArray['email'];
$customerREquiredArray['order id']= $orderID;
$customerREquiredArray['order date']= $createdAt;
$customerREquiredArray['products']= implode(",",$sku); // convert array to string