Magento – How to export orders in csv file

exportmagento-1.8magento-1.9sales-order

I'm new on Magento.

How to export orders with code SKU price etc.

I read the link below but didn't get where to add this code and how this code works please help.

Programmatically Export Sales order customer details to excel format

Best Answer

<?php
    require_once("app/Mage.php");
    Mage::app();
    Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
    $orders = Mage::getModel('sales/order')->getCollection();
    $orders->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE);
    $fp = fopen('export.csv', 'w');

    $header = array("Customer ID", "First Name", "Last Name", "Email", "City",  "Telephone", "Country", "Postcode", "Status");
    fputcsv($fp, $header);

    foreach($orders as $order) {
        $billingAddress = $order->getBillingAddress();
        $countryCode = $billingAddress->getCountryId();
        $country = Mage::getModel('directory/country')->loadByCode($countryCode);
        $countryName = $country->getName();

        $fields = array($order->getId(), $billingAddress->getFirstname(),$billingAddress->getLastname(), $billingAddress->getEmail(), $billingAddress->getCity(),  $billingAddress->getTelephone(), $countryName, $billingAddress->getPostcode(), $order->getStatus());
        fputcsv($fp, $fields);
    }
?>

Please try this, Its working for me

Related Topic