tags:

views:

3780

answers:

7

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

A: 

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.

Matt Haley
+6  A: 

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->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.

Manos Dilaverakis
+2  A: 

Hi,

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);

craig.michael.morris
+1  A: 

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

Very stupid system, why in the world they wouldn't include export?!?!

JohnyTheMAd
A: 

Hello,

this scripts works fine for me to export current orders but i also need to add purchaed item(s) to this current order export. Can any one provide me the code.

i am using following code as you mention above and it works for me perfectly but i am not able to include purchaed item(s) into export. Thanks in advance.

require_once '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(); //print_r($allIds);

foreach($allIds as $thisId) { //$myOrder->load($thisId); $myOrder->reset()->load($thisId); echo "

";
    print_r($myOrder);
    echo "
";

//Some random fields

echo "'" . $myOrder->getBillingAddress()->getFirstname() . "',";
echo "'" . $myOrder->getBillingAddress()->getLastname() . "',";
echo "'" . $myOrder->getTotal_paid() . "',";
echo "'" . $myOrder->getShippingAddress()->getTelephone() . "',";
echo "'" . $myOrder->getPayment()->getCc_type() . "',";
echo "'" . $myOrder->getStatus() . "',";
echo "\r\n";

}

manu
A: 

Hello,

Regarding export current orders, The above script works for magento 1.4.0 but does not work for 1.4.1.0 can some one tell me what i need to change in above script.

thanks in advance.

manu