Abbreviate State Names (Without Hacking the Database)

Modules, Add-ons and custom code that's more than just a quick hack or Mod.
Post Reply
CharlieFoxtrot
Confirmed
Confirmed
Posts: 413
Joined: Sun Aug 09, 2009 1:23 pm

Abbreviate State Names (Without Hacking the Database)

Post by CharlieFoxtrot »

Here's a code snippet that will easily convert US state names (and Canadian provinces) into their standard two-letter abbreviations.

This does not modify the database... it only makes the substitution whenever you're viewing orders on the "Admin:View Orders" page, or whenever you print out a Packing List or Invoice.

This has been tested and is working on ISC 4.07.

As always... make backups, test before going live.

1) Open the file admin\includes\classes\class.orders.php

2) Scroll to the bottom of the page and find the closing tag

FIND THIS:

Code: Select all

?>
3) Just above the closing tag... insert the following code

INSERT THIS ABOVE:

Code: Select all

// =====================================================
// Convert state / province names to two letter abbreviations
// =====================================================
function state_to_twoletter( $state_name, $country_name )  {
	if ($country_name == "United States" || $country_name == "Canada") {
		$state = array();
		$state['ALABAMA']='AL'; $state['ALASKA']='AK'; $state['AMERICAN SAMOA']='AS'; 
		$state['ARIZONA']='AZ'; $state['ARKANSAS']='AR'; $state['CALIFORNIA']='CA'; 
		$state['COLORADO']='CO'; $state['CONNECTICUT']='CT'; $state['DELAWARE']='DE'; 
		$state['DISTRICT OF COLUMBIA']='DC'; $state['FEDERATED STATES OF MICRONESIA']='FM'; 
		$state['FLORIDA']='FL'; $state['GEORGIA']='GA'; $state['GUAM']='GU'; $state['HAWAII']='HI'; 	
		$state['IDAHO']='ID'; $state['ILLINOIS']='IL'; $state['INDIANA']='IN'; $state['IOWA']='IA'; 
		$state['KANSAS']='KS'; $state['KENTUCKY']='KY'; $state['LOUISIANA']='LA'; $state['MAINE']='ME'; 
		$state['MARSHALL ISLANDS']='MH'; $state['MARYLAND']='MD'; $state['MASSACHUSETTS']='MA'; 
		$state['MICHIGAN']='MI'; $state['MINNESOTA']='MN'; $state['MISSISSIPPI']='MS'; 
		$state['MISSOURI']='MO'; $state['MONTANA']='MT'; $state['NEBRASKA']='NE'; 
		$state['NEVADA']='NV'; $state['NEW HAMPSHIRE']='NH'; $state['NEW JERSEY']='NJ'; 
		$state['NEW MEXICO']='NM'; $state['NEW YORK']='NY'; $state['NORTH CAROLINA']='NC'; 
		$state['NORTH DAKOTA']='ND'; $state['NORTHERN MARIANA ISLANDS']='MP'; $state['OHIO']='OH'; 
		$state['OKLAHOMA']='OK'; $state['OREGON']='OR'; $state['PALAU']='PW'; $state['PENNSYLVANIA']='PA'; 
		$state['PUERTO RICO']='PR'; $state['RHODE ISLAND']='RI'; $state['SOUTH CAROLINA']='SC'; 
		$state['SOUTH DAKOTA']='SD'; $state['TENNESSEE']='TN'; $state['TEXAS']='TX'; $state['UTAH']='UT'; 
		$state['VERMONT']='VT'; $state['VIRGIN ISLANDS']='VI'; $state['VIRGINIA']='VA'; 
		$state['WASHINGTON']='WA'; $state['WEST VIRGINIA']='WV'; $state['WISCONSIN']='WI'; 
		$state['WYOMING']='WY'; $state['ALBERTA']='AB'; $state['BRITISH COLUMBIA']='BC'; 
		$state['MANITOBA']='MB'; $state['NEW BRUNSWICK']='NB'; $state['LABRADOR']='NL'; 
		$state['NEWFOUNDLAND']='NL'; $state['NORTHWEST TERRITORIES']='NT'; $state['NOVA SCOTIA']='NS'; 
		$state['NUNAVUT']='NU'; $state['ONTARIO']='ON'; $state['PRINCE EDWARD ISLAND']='PE'; 
		$state['QUEBEC']='QC'; $state['SASKATCHEWAN']='SK'; $state['YUKON']='YT';
		return $state[strtoupper( $state_name )]; 
		}
	else {
		return $state_name;
		}
	}
4) Next... scroll up (just above the code you added) and find this:

FIND THIS:

Code: Select all

$addressPieces = array(
	isc_html_escape($address['shipfirstname']).' '.isc_html_escape($address['shiplastname']),
	isc_html_escape($address['shipcompany']),
	isc_html_escape($address['shipaddress1']),
	isc_html_escape($address['shipaddress2']),
	trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),
	isc_html_escape($address['shipcountry']).$countryFlag
);
5) We'll be working with the line that reads: trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),

The "shipstate" needs to be passed through our converter, but we ALSO need to let the converter check to confirm that the country is "United States" or "Canada". So... back to the code:

LOCATE THIS:

Code: Select all

trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),
REPLACE WITH:

Code: Select all

trim(isc_html_escape($address['shipcity'].', '.state_to_twoletter($address['shipstate'],$address['shipcountry']).' '.$address['shipzip']), ', '),
6) Save, Upload, and TEST!!

This could be easily modified to include any standard postal abbreviations for other countries as well.

Enjoy! 8-) Buy me a beer: paypal to... charlie.foxtrot.ebay [at] xemaps.com 8-)
ISC 4.0.7

"... and let's be honest that whole "by design" thing is getting old too."
Martin
Site Admin
Site Admin
Posts: 1854
Joined: Wed Jun 17, 2009 6:30 pm
Location: South Yorkshire UK
Contact:

Re: Abbreviate State Names (Without Hacking the Database)

Post by Martin »

Just a thought but you could save yourself a little code and work by using this format...

Code: Select all

function state_to_twoletter( $state_name, $country_name )  {
	if ($country_name == "United States" || $country_name == "Canada") {
		$state = array(
			'ALABAMA' => 'AL',
			'ALASKA' => 'AK', 
			'AMERICAN SAMOA' => 'AS', 
.... 
			'QUEBEC' => 'QC',
			'SASKATCHEWAN'] => 'SK',
			'YUKON']='YT'
		);
		return $state[strtoupper( $state_name )]; 
		}
	else {
		return $state_name;
		}
	}
Just a suggestion for ease of reading and also optimisation... :)
CharlieFoxtrot
Confirmed
Confirmed
Posts: 413
Joined: Sun Aug 09, 2009 1:23 pm

Re: Abbreviate State Names (Without Hacking the Database)

Post by CharlieFoxtrot »

Martin wrote:Just a thought but you could save yourself a little code and work by using this format...

Code: Select all

(...)
'ALABAMA' => 'AL',
'ALASKA' => 'AK', 
'AMERICAN SAMOA' => 'AS', 
(...)
Just a suggestion for ease of reading and also optimisation... :)
I like! ~ Thank you!! :D
ISC 4.0.7

"... and let's be honest that whole "by design" thing is getting old too."
Post Reply