2

Background :

I can able to download excel sheet with information of customer's Id , name....

enter image description here

Requirement :

How to get Customer's First Order id in excel ?

<?php
require_once('app/Mage.php');
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');   

    //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');
    $addedKeys = false;
    do{
         //Tell the collection which page to load.
         $collection->setCurPage($currentPage);
         $collection->load();
         foreach ($collection as $customer){
            //write the collection array as a CSV.
            $customerArray = $customer->toArray();
            $customerREquiredArray['customer ID'] = $customerArray['entity_id'];
            $customerREquiredArray['First name'] = $customerArray['firstname'];

            if($addedKeys == false){
                $keys = array_keys($customerREquiredArray);
                fputcsv($fp, $keys);
                $addedKeys = true;
            }
            //var_dump($customerArray); echo "\n\n";
            fputcsv($fp, $customerREquiredArray);
            //fwrite($fp,print_r($customerArray,true) . chr(10) );
            $customerCount++;
         }
         $currentPage++;
         //make the collection unload the data in memory so it will pick up the next page when load() is called.
         $collection->clear();
         //break; //DEBUG
         echo "Finished page $currentPage of $pages \n"; 
    } while ($currentPage <= $pages);
    fclose($fp);
} catch (Exception $e) {
    //$response['error'] = $e->getMessage();
    Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
  • Maybe this will help (in Database it call increment_id) Try this LINK – Marcel Jul 18 '18 at 11:49
  • @Marcel Thanks for the link, but i need to fetch order id based on customer id.... – Baby in Magento Jul 18 '18 at 12:24
1

You can get first customer id with following code

foreach ($collection as $customer){
   $newordercollection = Mage::getModel("sales/order")->getCollection()->addFieldToFilter('customer_id',$customerArray['entity_id']);

  $collection->setOrder('entity_id', 'ASC');
  $firstItem = $newordercollection->getFirstItem();
  $orderID = $firtItem->getIncrementId();
  $customerArray = $customer->toArray();
        $customerREquiredArray['customer ID'] = $customerArray['entity_id'];
        $customerREquiredArray['First name'] = $customerArray['firstname'];
       $customerREquiredArray['First Order'] = $orderID;

        if($addedKeys == false){
            $keys = array_keys($customerREquiredArray);
            fputcsv($fp, $keys);
            $addedKeys = true;
        }
        //var_dump($customerArray); echo "\n\n";
        fputcsv($fp, $customerREquiredArray);
        //fwrite($fp,print_r($customerArray,true) . chr(10) );
        $customerCount++;

}
  • @BabyinMagento update the code. – Qaisar Satti Jul 19 '18 at 10:24
  • yes, its displaying last 3 digits in order id, please help me for complete order id..... – Baby in Magento Jul 19 '18 at 10:27
  • example : instead of 100000122 , its displaying 122 only.... – Baby in Magento Jul 19 '18 at 10:28
  • here is full code : pastebin.com/AqeL5gpL – Baby in Magento Jul 19 '18 at 10:31
  • @BabyinMagento updated the code.. – Qaisar Satti Jul 19 '18 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.