1

I want data from the order table which contain products related to category ID(80). This is the category ID. And want data From: 2012-11-17 to 2015-9-15. Data I needed (Name, email, shipping address, phone number.)

2
  • from 15 sept 2015 to till date? Jan 13, 2017 at 6:07
  • I looked into these three table "sales_flat_order" , "sales_flat_order_address", "sales_flat_order_item" for joining. But not getting perfect data.
    – Ravi Soni
    Jan 13, 2017 at 6:19

2 Answers 2

3

Place this code on your root folder :

  <?php
require_once 'app/Mage.php';
Mage::app();
$catId = 46; 
$outputFile = 'var/myfile.csv'; //csv file location
$write = fopen($outputFile, 'w');
$fromDate = date('Y-m-d H:i:s', strtotime('12/07/2015')); // eg. 12/07/2015
$toDate = date('Y-m-d H:i:s', strtotime('12/07/2017'));

$orderCollection = Mage::getModel('sales/order')
                    ->getCollection()
                    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))
                    ->load();
foreach($orderCollection as $order)
{
    $order = Mage::getModel("sales/order")->load($order->getId());
    $orderIncrementId= $order->getIncrementId();
    $orderObj = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    $orderItems = $orderObj->getAllVisibleItems();
    foreach($orderItems as $i):
        $productId = $i->getProductId(); // get product id from each line item
        $product = Mage::getModel('catalog/product')->load($productId);
        $categoryIds = $product->getCategoryIds();
    endforeach;

    if (in_array($catId, $categoryIds))
    {
        // put your logic here for get info 
        $data = array($order->getId(),$order->getCustomerName(),$order->getCustomerEmail());
        fputcsv($write, $data);

    }
}
fclose($write);
?>

[Edit]

Here is very simple code for save data in csv.

<?php 
       $outputFile = 'var/myfile.csv'; //csv file location
       $mage_csv = new Varien_File_Csv(); //mage CSV   
       $mainArray = array();
       foreach($ordercol as $order)
       {
         $data = array();
         $data['id'] = $order->getId();
         $data['name'] = $order->getCustomerName();
         $data['email'] = $order->getCustomerEmail();
          $mainArray[] =  $data;
       }
       $titles = array('id','name','email'); // for set the field name
       array_unshift($mainArray,$titles);
       $mage_csv->saveData($outputFile, $mainArray);

       ?>
5
  • Can you please put code where I can generate csv file. Thank you in advance.
    – Ravi Soni
    Jan 13, 2017 at 7:59
  • @Ravi updated the code for csv
    – Arunendra
    Jan 13, 2017 at 8:09
  • Only one record I got in csv file where I found 336 data. And trying to debug but not getting. Also need column field name. Thank you in advance.
    – Ravi Soni
    Jan 13, 2017 at 8:45
  • @Ravi I'm getting all record in my csv and updated the code for save date in csv you can customize it according to your need thanks
    – Arunendra
    Jan 13, 2017 at 9:39
  • Thank you Arunendra sir, I debug the code actually it's not working in IF condition for cat_id. But fine I will manage it. Thanks again.
    – Ravi Soni
    Jan 13, 2017 at 10:06
0

Follow these 3 steps:

  1. Get order collection based on date filter.
  2. In Foreach loop, load order and get Items of order.
  3. Get category of Items and place a condition if category Id is 80 then get order Id, customer name, customer email.

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.