So I have this script:

ini_set('display_errors', 1);


/* tell the browser that we are a csv file */
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=orders.csv");
header("Pragma: no-cache");
header("Expires: 0");

/* Set our dates */
$fromDate = '2016-05-20 00:00:00';
$toDate = '2019-01-01 00:00:00';

/* Get the collection. Here we set a date range, and ignore cancelled orders.
   You can filter the collection however you prefer */
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))
    ->addFieldToFilter('status', array('nin' => array('canceled')));

/* Here you can build an array, print to screen, write to a CSV etc */
echo "Order ID,Customer Name,Order Total".PHP_EOL;
foreach($orders as $o){

    /* lets output the required data in csv format */
    echo $o->getIncrementId().',';
    echo $o->getCustomerName().',';
    echo $o->getBaseGrandTotal();
    echo PHP_EOL;

    /* you can load the order items, if you need them */
    //$items = $order->getAllVisibleItems();

I want to get the sku, products ordered, quantity ordered. How can I do that?

1 Answer 1


You can use below code to get Product Info. If you want more Information about product the you can load that product inside foreach loop.

$items = $o->getAllVisibleItems();
foreach($items as $i):
    echo $i->getProductId();
     echo $i->getSku();


  • Where can I put that into my code?
    – DevAnd
    Aug 22, 2017 at 21:00
  • where you comment out line $items = $order->getAllVisibleItems(); }. Inside Order Loop Aug 22, 2017 at 21:02
  • Also how can I separate each?
    – DevAnd
    Aug 22, 2017 at 21:02
  • Please accept my answer if its useful. Aug 22, 2017 at 21:09
  • I haven't been able to use your code. The output is too messy.
    – DevAnd
    Aug 22, 2017 at 21:26

Your Answer

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

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