Search code examples
phpmagentoexport

Export Orders from Magento for shipment


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.


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