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.

function
getStateAbbreviation( $stateIn ) {
  $stateArray = array (
    "ALABAMA"=>"AL",
    "ALASKA"=>"AK",
    "AMERICAN SAMOA"=>"AS",
    "ARIZONA"=>"AZ",
    "ARKANSAS"=>"AR",
    "ARMED FORCES AFRICA"=>"AE",
    "ARMED FORCES AMERICAS"=>"AA",
    "ARMED FORCES CANADA"=>"AE",
    "ARMED FORCES EUROPE"=>"AE",
    "ARMED FORCES MIDDLE EAST"=>"AE",
    "ARMED FORCES PACIFIC"=>"AP",
    "CALIFORNIA"=>"CA",
    "COLORADO"=>"CO",
    "CONNECTICUT"=>"CT",
    "DELAWARE"=>"DE",
    "DISTRICT OF COLUMBIA"=>"DC",
    "FEDERATED STATES OF MICRONESIA"=>"FM",
    "FLORIDA"=>"FL",
    "GEORGIA"=>"GA",
    "GUAM"=>"GU",
    "HAWAII"=>"HI",
    "IDAHO"=>"ID",
    "ILLINOIS"=>"IL",
    "INDIANA"=>"IN",
    "IOWA"=>"IA",
    "KANSAS"=>"KS",
    "KENTUCKY"=>"KY",
    "LOUISIANA"=>"LA",
    "MAINE"=>"ME",
    "MARSHALL ISLANDS"=>"MH",
    "MARYLAND"=>"MD",
    "MASSACHUSETTS"=>"MA",
    "MICHIGAN"=>"MI",
    "MINNESOTA"=>"MN",
    "MISSISSIPPI"=>"MS",
    "MISSOURI"=>"MO",
    "MONTANA"=>"MT",
    "NEBRASKA"=>"NE",
    "NEVADA"=>"NV",
    "NEW HAMPSHIRE"=>"NH",
    "NEW JERSEY"=>"NJ",
    "NEW MEXICO"=>"NM",
    "NEW YORK"=>"NY",
    "NORTH CAROLINA"=>"NC",
    "NORTH DAKOTA"=>"ND",
    "NORTHERN MARIANA ISLANDS"=>"MP",
    "OHIO"=>"OH",
    "OKLAHOMA"=>"OK",
    "OREGON"=>"OR",
    "PALAU"=>"PW",
    "PENNSYLVANIA"=>"PA",
    "PUERTO RICO"=>"PR",
    "RHODE ISLAND"=>"RI",
    "SOUTH CAROLINA"=>"SC",
    "SOUTH DAKOTA"=>"SD",
    "TENNESSEE"=>"TN",
    "TEXAS"=>"TX",
    "UTAH"=>"UT",
    "VERMONT"=>"VT",
    "VIRGINIA"=>"VA",
    "VIRGIN ISLANDS"=>"VI",
    "WASHINGTON"=>"WA",
    "WEST VIRGINIA"=>"WV",
    "WISCONSIN"=>"WI",
    "WYOMING"=>"WY"
    );
 
    $key = strtoupper($stateIn);
    return( $stateArray[$key] );
}

Just a quickie, but it sure does help.