Site icon MageComp Blog

How to Export Specific Custom Orders Data Field in CSV from Magento

How to Export Custom Orders CSV using given orders id in Magento 2

Having thousands of orders inside your Magento Store makes difficult to look for a specific order data even if you use a backend filter system. And, manually going through all order details is not so easy because there is also so many unnecessary other order related information along with data for which you are actually looking for. Many times it happens that as a store owner, you need very specific data that you have to share with your shipping provider or external services that you are using for your customer. Magento backend doesn’t allow you to export such data from the backend.
At that time getting help from Magento Expert Developer will help you to quickly export such data using some sort of scripts or code. We have come across so many requirements to export some specific order data from Magento. That’s why we are back with another blog series that will help you to quickly export last any number of data with only required fields.
To do the same first we need to create a “orderexport.php” php script using below code.


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

try {
    $orders = Mage::getModel('sales/order')->getCollection()
        ->setOrder('created_at', 'desc')
        ->setPageSize(10); //export data from last give orders id  to 10 ex.
    $fp = f0pen(Mage::getBaseDir() . "/orderexport.csv", "w+");
    $csvHeader = array('Order ID', 'Customer Email', 'Customer First Name', 'Customer Last Name'); //CSV Header title
    fputcsv($fp, $csvHeader, ","); //save sequence as give csv header title add more to add both side.
    foreach ($orders as $order) {
        fputcsv($fp, array(
            //save sequence as give csv header title add more to add both side.
            $order->getIncrementId(),
            $order->getCustomerEmail(),
            $order->getCustomerFirstname(),
            $order->getCustomerLirstname()
        ));
    }
    fclose($fp);
    echo 'orderexport.csv Successfully created.';

}catch (\Exception $e){
    echo $e->getMessage();
}
?>

Place this script file in the root server of your Magento and run it by hitting file URL.
I.e. yourstoreurl.com/orderexport.php
That’s it, once a successful script runs it will export order data in CSV file with specified field data. You can find that “orderexport.csv” in the root folder of your hosting. You are free to add or remove order fields that you want to export from your Magento.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.
Happy Coding!

Exit mobile version