Magento 1.9: Custom CSV Export for Last 1000 Orders – How to

csvdatabaseexportmagento-1.9

I would like to export the last 1000 orders with following fields:
order id, customer email, first name, last name.

Is there an easy way to export a CSV like this? I do have access to PHPMyAdmin.

Best Answer

Here is the script to export last 1000 orders into csv format. To use this script just create test.php in your magento root directory and paste this code and run this script.

<?php
ini_set('display_errors', 1);
require_once('app/Mage.php');
Mage::app();

$orders = Mage::getModel('sales/order')->getCollection()
    ->setOrder('created_at', 'desc')
    ->setPageSize(1000); // from here you can update the order count
$fp = fopen(Mage::getBaseDir() . DS . "exports_order.csv", "w+");
$csvHeader = array('Order ID', 'Customer Email', 'Customer First Name', 'Customer Last Name');
fputcsv($fp, $csvHeader, ",");
foreach ($orders as $order) {
    fputcsv($fp, array(
        $order->getIncrementId(),
        $order->getCustomerEmail(),
        $order->getCustomerFirstname(),
        $order->getCustomerFirstname()
    ));
}
fclose($fp);

echo 'exports_order.csv Successfully created.';
exit;
Related Topic