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

Was it helpful?

Solution

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

OTHER TIPS

I was in the process of implementing your solution and noticed that it was only returning the first values for all the foreign keys such as billing address, shipping address, payment etc...

This can be fixed by changing

$myOrder->load($thisId);

to

$myOrder->reset()->load($thisId);

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>"; 
?>

In case this helps any one, you can use the invoice table as a key. An invoice in magento with a auth+capture credit card setup means the money has come in. In our case we only needed a sql query to run in phpmyadmin that would export order numbers and invoice numbers for us to reconcile to check that the order export extension from xtento was working. This is what we used:

 SELECT sales_flat_order.increment_id AS 'order', sales_flat_invoice.increment_id AS 'invoice'
   FROM sales_flat_order
  RIGHT JOIN sales_flat_invoice ON sales_flat_invoice.order_id = sales_flat_order.entity_id
  WHERE sales_flat_invoice.updated_at >= "2011-07-01 00:00:00"
  ORDER BY sales_flat_order.increment_id DESC 

If the system doesn't support any direct way to export orders, you could create a view in the database that lists the orders you need to export. Then use something like phpMyAdmin to export the data from the view as CSV.

Saho's suggestion for using SOAP is great, but it may take a long time (Apache can only assign a limited CPU resource to tread to handle that request)

Suggest you to write a php script and then run it through terminal.

Sonam

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top