Magento – Export Orders – A function to return USPS state abbreviation

It seems the Magento SOAP API is only returning the full state name to me (country being United States). I know that Magento knows the abbreviation – I see it in the source on the customer/address/edit page.

But I’m only getting the full state name via SOAP for my SOAP Order Exporter. No matter, here’s a function that’ll return the state abbreviation. Thank you to the USPS for the state listing. The array is all uppercase because that’s how it came in from the USPS, and I just pasted into my vi editor and used it as-is.

getStateAbbreviation( $stateIn ) {
  $stateArray = array (
    "NEW JERSEY"=>"NJ",
    "NEW MEXICO"=>"NM",
    "NEW YORK"=>"NY",
    "PUERTO RICO"=>"PR",
    $key = strtoupper($stateIn);
    return( $stateArray[$key] );

Just a quickie, but it sure does help.

Magento – Export Orders


Aug 4, 2009: This article refers to the version 1 SOAP interface. The version 2 interface handles things a bit more cleanly; structs are passed back and forth instead of associative arrays in many cases. You can visit my WSDL dump to see some of the differences between the two:


I was at least mildly surprised that Magento did not have a built-in order export module.

Some folks need to export to QuickBooks; others, I guess, just like to have a spreadsheet format to shake and bake to their heart’s content. Still others, like me, need to export orders for consumption by a warehousing (“ERP”) and distribution system. I am building orders for a system that handles all warehousing and shipping duties. It’s imperative that orders coming from Magento are consumable by the distribution system to actually distribute the product!


Sebastian Enzinger has a fully Magento-integrated module here, and I highly recommend you give it a look to see if it suits your needs. My solution here … well, it doesn’t come close to Sebastian’s that’s the truth.  It is, however, a fully Magento-correct SOAP API implementation. The difference – I’m running mine Warehouse System side.

It’s a lot easier for me in this case to have the already trained staff take a menu option from their familiar Text Screen interface than it is to ask them to log into a web site, select an option, hop back to the Text Screens and – well, you get the idea.

That’s the magic of SOAP. There is nothing I can do short of rolling my own WSDL implementation on a legacy system that can’t even speak SQL that will work for me. Rather, Magento having so lovingly crafted a WSDL and SOAP API, allows me to let the legacy system call Magento for orders.


The Magento SOAP API is, to put it kindly, sparsely documented. But I’m not complaining. The thing is terrific in what it does; I’m happy to let documentation (such as this post, come to think of it) catch up with the fabulous engine under the hood. It is, in fact, more well documented than the CyberSource SOAP API (See also: ). Some may consider that damning with faint praise; I don’t mean it that way. But if you’re not a SOAP/ php/ Java or what-have-you-guru, you’ll have some exciting challenges before you. Even if you are a guru, you could be in for some excitement.

Some Code

In php. Refer to’s excellent tutorial on Consuming Web Services via SOAP to get your Magento instance ready for this code.

First, though, a function that turns an array into an object. I like to make an object from the arrays, it makes the code a little easier to read, and a lot easier to write. So here’s a function to do just that:

array_to_obj($array, &$obj)
    foreach ($array as $key => $value) {
    if (is_array($value)) {
        $obj->$key = new stdClass();
        array_to_obj($value, $obj->$key);
    else {
        $obj->$key = $value;
  return $obj;

Ok, that’s out of the way. Let’s get hooked into Magento:

    $proxy = new SoapClient('YourMagentoURL/index.php/api/soap/?wsdl');
    $sessionId = $proxy->login('webServiceUsername', 'webServicePassword');

Next, let’s get a list of orders in status “Processing” to work with:

    $orderListRaw = $proxy->call($sessionId, 'sales_order.list',

$orderListRaw now contains an array of – well, let’s call them Order Headers. So let’s loop through these headers (my term) and grab the whole order from them.

    $howManyOrders = count($orderListRaw);
    for( $i = 0; $i < $howManyOrders; $i++ ) {
       array_to_obj( $orderListRaw[$i], $orderHead );
       $orderAsArray = $proxy->call($sessionId, '',
                   $orderHead->increment_id );
       array_to_obj( $orderAsArray, $o );

At this point, “$o” is a complete order, as an object. Let me grab the billing_address info:

    $bi = $o->billing_address; // Shorthand the billing address info

First name, Last Name, Company and address are now available as:


Similarly, shipping information is here:

    $si = $o->shipping_address;

For line item details, I go back to the $orderListRaw array, and pull out the array of items it contains. This is simpler to me because I can simply “count()” the number of elements in the items array to know how many line items are on this order. I loop through the line items thus:

    $lineItems = $orderAsArray["items"];
    $numberOfLines = count( $lineItems );
    for( $j = 0; $j < $numberOfLines; $j++ ) {
       array_to_obj( $lineItems[$j], $li );
       /* Now I have fields: $li->sku, $li->price, $li->base_row_total ... */
      /* Do something with line items here ...*/

Lastly, I need payment and shipping information, which I can access like so:


There are many fields available to you, and many permutations of the data you can export. If you have any comments, questions or suggestions, drop me a line!