I am having issues with my Magento site and need to pull sales orders from my Magento database. Can anyone help with this?

  • where you want to pull all the magento orders? Aug 21 '17 at 15:23
  • Not sure what you mean? I just want to pull that info so I can have.
    – DevAnd
    Aug 21 '17 at 15:33
  • I mean in which file you want to pull all the orders? e.g Product Page, Category Page Aug 21 '17 at 15:34
  • Oh I am not sure then. My site is having issues so I cannot see anything on admin so was hoping I could get that info. What do you recommend?
    – DevAnd
    Aug 21 '17 at 15:35
  • If your admin is not working then what would you do with All Order Information ? Aug 21 '17 at 15:36

This is a simple way to get you started, put this script in the root of the site.

You can pick and choose which fields you want inside the loop. If you are dealing with a large amount of orders this will be slow, and you might want to look into defining a custom walker function (http://inchoo.net/magento/working-with-large-magento-collections/).

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();

edit: expanded solution

  • How would it be if I were to want it as a CSV?
    – DevAnd
    Aug 21 '17 at 15:44
  • I would probably build an array inside the orders loop, then use fputcsv to write it to a file.
    – James Sims
    Aug 21 '17 at 15:45
  • Do you think you can help me with that? Ouput to the screen is fine.
    – DevAnd
    Aug 21 '17 at 20:43
  • What data do you need? e.g. order number, customer name, total value?
    – James Sims
    Aug 22 '17 at 8:34
  • Yeah sounds about right
    – DevAnd
    Aug 22 '17 at 13:10

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.