Magento Custom Order Exporting

By Michael Kramer on October 3rd 2014 @ 9:21 am

For one of my clients, I needed to setup Custom Order Exporting that would export a CSV file in the proper format once a night (at midnight).

This is accomplished through the Magento Cron Interface, you create a custom Module and in your config.xml you add:

                <schedule><cron_expr>0 0 * * *</cron_expr></schedule>

spyder = is my package 
export = my module
Also, very important, is under the section of your config.xml, you need to define where the models come from


In the section I'm telling it to run once a night at midnight, and run the function export/cron::sendOrders which is Export/Cron.php and the function sendOrders();
That sets up our cron, now we need to create the actual function to send the orders.

Getting the order collection:

$orders = Mage::getResourceModel('sales/order_collection');

This retrieves all the sales for your store, however I only want the ones that are "processing"


I then want to grab the Id's and loop through them,

$allIds = $orders->getAllIds();
foreach($allIds as $thisId) {

While looping through them, I want to get each individual order object so i can then get the information I need, this is accomplished by loading the order. Outside of the foreach loop you need to define the model,

$_order = Mage::getModel('sales/order');

Then in the foreach loop, you want to load the order


We then want to update the status of this order to complete, as we are processing this order

$_order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true, 'Sending to fullfillment', null , false);

From here you can get all the information related to the order, below are some helpful commands to keep in mind.

Get all items

$_items = $_order->getAllItems();

Get Order ID


Get Shipping Address


Get Billing Address


Get Customer Email


Get Order Date


Get Shipping Method

comments powered by Disqus