Export Orders from Magento for shipment


Question

I am working on an online store on the Magento platform and have hit a major roadblock: For some reason I cannot figure out how to export current orders (with shipping information/shipment type/etc). Does anyone have any suggestions? This seems as if it should be one of the most basic things for a system like this to do, but I have not been able to find out how.

Thank you in advance for your help,

Andy

1
9
9/26/2015 4:22:02 AM

Accepted Answer

Seeing as you want this for shipping you might want to ask whoever handles your shipping whether they have some sort of API so you can build/buy/download an appropriate shipping module and spare yourself the hassle of mucking about with CSV files.

If you really want a CSV file however I can show you how to create it. You didn't mention where this script will run so I'll assume it's an external script (which will make it easier to use with a cron job). You want to do the following:

//External script - Load magento framework
require_once("C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\magento\app\Mage.php");
Mage::app('default');

$myOrder=Mage::getModel('sales/order'); 
$orders=Mage::getModel('sales/mysql4_order_collection');

//Optional filters you might want to use - more available operations in method _getConditionSql in Varien_Data_Collection_Db. 
$orders->addFieldToFilter('total_paid',Array('gt'=>0)); //Amount paid larger than 0
$orders->addFieldToFilter('status',Array('eq'=>"processing"));  //Status is "processing"

$allIds=$orders->getAllIds();
foreach($allIds as $thisId) {
    $myOrder->reset()->load($thisId);
    //echo "<pre>";
    //print_r($myOrder);
    //echo "</pre>";

    //Some random fields
    echo "'" . $myOrder->getBillingAddress()->getLastname() . "',";
    echo "'" . $myOrder->getTotal_paid() . "',";
    echo "'" . $myOrder->getShippingAddress()->getTelephone() . "',";
    echo "'" . $myOrder->getPayment()->getCc_type() . "',";
    echo "'" . $myOrder->getStatus() . "',";
    echo "\r\n";
}

For the sake of brevity (and sanity) I haven't listed all the available order information. You can find out what fields are available by dumping the relevant objects and taking a look at their fields.

For example if you were to do print_r($myOrder->getBillingAddress()); you'd see fields like "address_type" and "lastname". You can use these with $myOrder->getBillingAddress()->getAddress_type() and $myOrder->getBillingAddress()->getLastname() respectively.

Edit: Changed code according to craig.michael.morris's answer

24
4/29/2014 5:01:29 PM

You may also want to look at this extension: http://www.magentocommerce.com/extension/1158/manual-order-export

Also you can connect via soap: This example is set up for localhost and assumes you have set up a web services user and role under system>>web services in the admin.

    <?php 
$time = microtime(); 
$time = explode(' ', $time); 
$time = $time[1] + $time[0]; 
$begintime = $time; 
?> 
<?php 
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 
// hostname 
$host= '127.0.0.1'; 
// if store in /magento change /shop, if store in root remove /shop 
$client= new SoapClient('http://'.$host.'/magento/index.php/api/soap/?wsdl'); 

// Can be added in Magento-Admin -> Web Services with role set to admin 
$apiuser= 'soap'; 
// API key is password 
$apikey = '******'; 
$sess_id= $client->login($apiuser, $apikey); 
echo "<html>"; 
echo "<head>"; 
echo "<LINK REL=StyleSheet HREF=\"style.css\" TYPE=\"text/css\" MEDIA=screen>"; 
echo "</head>"; 
echo "<body>";

$result= $client->call($sess_id, 'sales_order.list',  array(array('status'=>array('='=>'Pending'))));
echo '<pre>';
print_r($result);
echo '<pre>';


?> 
<?php 
// Let's see how long this took… 
$time = microtime(); 
$time = explode(" ", $time); 
$time = $time[1] + $time[0]; 
$endtime = $time; 
$totaltime = ($endtime - $begintime); 
echo '<br /><br /><em>This Magento SOAP API script took ' .$totaltime. ' seconds, precisely.</em>'; 
// ...and close the HTML document 
echo "</body>"; 
echo "</html>"; 
?>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon