6

Background :

I can able to display the Customer's Orders in excel sheet....

enter image description here

Requirement :

Now I am trying to get the Ordered Product skus in excel sheet...

Issue :

Notice: Undefined variable: sku

<?php

$customerCount = 0;
try{
    //configure the collection filters.
    $collection = Mage::getResourceModel('customer/customer_collection')
    ->addAttributeToSelect('entity_id')
    ->addAttributeToSelect('firstname') 
    ->addAttributeToSelect('email');

    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');
  $firstItem = $newordercollection->getFirstItem();
  $orderID = $firstItem->getIncrementId();

        foreach ($newordercollection as $orderkey => $ordervalue)
        {           
             foreach ($ordervalue->getAllVisibleItems() as $item) 
              {  
                $name = $item->getName(); 
              }      
              $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['products']= $name;
            $customerCount++;
    }
}

Full code : https://pastebin.com/2bHGSZiw

2
  • Do you want to show products by orders or all products a customer ordered in one single column.? Commented Jul 23, 2018 at 5:23
  • @JaiminSutariya Thanks for reply, i want to show only one product of that order.... means if in that order, there are 2 products, than i want to display the product which is created first [ i need to fetch using product created date ] Commented Jul 23, 2018 at 5:26

2 Answers 2

4
+100

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 enter image description here

10
  • Let me know if you face any error with this code Commented Jul 23, 2018 at 6:09
  • Thanks , it worked fine & displaying all the products of that order, can you please help me to display only one product of that order [ which product created first, i want to display that product only.... ] Commented Jul 23, 2018 at 6:15
  • okay you don't need all products ? i will update answer soon Commented Jul 23, 2018 at 6:22
  • check my updated answer :) Commented Jul 23, 2018 at 6:26
  • That is gold, thanks a lot , please help me to display Product created date & time in next column.... Commented Jul 23, 2018 at 6:33
2

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
1
  • sorry, still it gave me same error , anyway thanks for your time , i got solution from other answer..... Commented Jul 23, 2018 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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