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:

  <crontab>
        <jobs>
            <spyder_export_all>
                <schedule><cron_expr>0 0 * * *</cron_expr></schedule>
                <run>
                    <model>export/cron::sendOrders</model>
                </run>
            </spyder_export_all>
        </jobs>
    </crontab>

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

  <models>
    <export>
      <class>Spyder_Export_Model</class>
    </export>
  </models>

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"

$orders->addFieldToFilter('status',Array('eq'=>"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

$_order->load($thisId);

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

$_order->getRealOrderId()

Get Shipping Address

$_order->getShippingAddress()

Get Billing Address

$_order->getBillingAddress()

Get Customer Email

$_order->getCustomerEmail()

Get Order Date

$_order->getCreatedAtFormated('long')

Get Shipping Method

$_order->getShippingMethod();
comments powered by Disqus